Jump to content

Best way to organize namespaced classes in the filesystem? (autoload question)


mkz

Recommended Posts

Hello, I'm new to the forum and I'm looking for advice.

 

I use a bunch of classes in namespaces that are I'm attempting to organize for autoloading. In other words, a class called \Foo\Package\Class is loaded from the file at Foo\Package\Class.php. I'm using spl_autoload for this. (If I were on 5.2 I could be using underscore-delimited pseudo-namespaces just as well; the implementation detail isn't important)

 

Now, I want to have multiple separate apps that use a single common library. Each app also has some classes that are local to it. How should I solve the autoloading problem?

 

I thought of the following approach. Is it any good?

[*]Try autoloading from the common class library

[*]If it fails, try autoloading from the app's own local library

 

Part 2 of the question:

 

Within my apps, though, there are two types of classes: library ("vendor") classes that are only being used in that particular app so they don't need to be in the shared library, and app-specific classes that are the core of the app (so they, by definition, don't need to be in any shared library). I'd probably like to keep these two separate, so I'd need to add point #3 to the list above: search in the "core" class hierarchy of the local app. This gives 3 separate locations, and the problem is that they negate the advantages namespaces since there can be overlap. In which case one class will override the other during autoloading. So order of the above list would matter. And time would be wasted looking for a class in the first two places if it's more often in the third.

 

A solution I was considering is sticking to just one central library of classes (and dumping all app-local libraries there). Then, the core classes that belong to one app would be under an \AppName namespace.

 

I'm looking forward to some insights from the experts. How do you guys organize your class libraries?

Link to comment
Share on other sites

I was hoping to find someone to give some advice. The topic isn't too advanced for you guys, is it?  :P

 

I can try to shorten the question!

 

[*]Does anyone use PHP's class autoloading features? If so, what are your best practices? If not, why not?

[*]If you have a library of autoloaded PHP classes, what's the best way to separate the common lib from the application lib?

  • I considered cascading include paths (like Kohana) but that violates the principle that namespaces correspond to the physical class structure. Any other solutions?

 

 

 

Link to comment
Share on other sites

This is what you need: http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1 . If you implement this class naming standard and file organization, you will be able to use any third party autoloader that supports it.

 

There is absolutely no need to scan the application directory first, and then the system's one for several reasons. Autoloaders should never use file_exists() function, because it is not aware of the include_path setting and can report that a file does not exist, even if it exists in fact and can be loaded with require(). The better (and faster) way is to extract the first part of the namespace and check what path is associated to it. Your application and system files simply lie in different namespaces and if you want to overwrite one of the system components, you should do this by overwriting the service that creates it or through the configuration.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.