Jump to content

[SOLVED] Autoloads for classes?


LemonInflux

Recommended Posts

Hello. I've been slowly migrating to PHP5, and I've been looking at the __autoload() function. I've been wondering about this, done a little searching, and have yet to find a definite solution.

 

My question is, can you redefine __autoload()?

 

Say I have a class called core. In core, I want this:

 

function __autoload($class) {
include_once dirname(__FILE__) ."/$class.class.php";
}

 

Now, in another class, called view, I want this:

 

function __autoload($class) {
include_once dirname(__FILE__) ."/views/$class.class.php";
}

 

Is this possible? Before you ask, no I can't check it, because I have no way of testing it today :(

 

Thanks in advance

 

----------------

Now playing: Linkin Park - Plc.4 Mie Haed (Amp Live ft. Zion)

via FoxyTunes

Link to comment
Share on other sites

While I'm not sure about the exact answer to your question I have implemented something similar in the past...

 

I used naming conventions to define where to autoload from, for example NewsController would be loaded from the controller directory and NewsModel would be autoloaded from the model directory. Of course you need to be careful about security with an implementation like this, and it's definitely not what I use to load my controllers and models, it's just an example.

 

Something to consider perhaps?

Link to comment
Share on other sites

http://en.wikipedia.org/wiki/Namespace_(computer_science)

 

I'm not sure how php6 will implement their namespaces though so holding off might not be the best bet.

 

Another alternative to a naming convention is that you could have an ini file or a php config file with the locations of the "special case" classes that the autoload consults before loading the class.

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.