LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Author Share Posted August 2, 2008 BUMP ---------------- Now playing: Linkin Park & Jay-Z - Points Of Authority / 99 Problems / One Step Closer via FoxyTunes Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 2, 2008 Share Posted August 2, 2008 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? Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Author Share Posted August 2, 2008 Thanks, but it's not really what I'm looking for. I don't want to have to use a function, and the autoload() allows naming conventions. I just want to try, if possible, to use autoload(). ---------------- Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat via FoxyTunes Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 2, 2008 Share Posted August 2, 2008 Just checked, no you can't redefine __autoload() Better answer? Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Author Share Posted August 2, 2008 Ugh, even if it's in different classes? ---------------- Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat via FoxyTunes Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 2, 2008 Share Posted August 2, 2008 Unfortunately autoload() is a procedural function... you would probably need namespaces for that. Namespaces are in php6 though last time I checked. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Author Share Posted August 2, 2008 Namespaces..? ---------------- Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat via FoxyTunes Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 2, 2008 Share Posted August 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Author Share Posted August 2, 2008 Aah, I see. Thanks then, topic closed. ---------------- Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat via FoxyTunes Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.