Best way of __autoload'ing from several folders?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Best way of __autoload'ing from several folders?

Post by kaisellgren »

Hi there,

I'm interested in knowing how people are __autoload'ing from several different folders. Do you have a class that caches the paths of the files, or do you perhaps do something like database_mysql where _ gets replaced by / and then it becomes database/mysql.php ? or do you setup a static variable or an ini file or ... ? So what is the way you tell your autoload where certain classes are located?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Best way of __autoload'ing from several folders?

Post by John Cartwright »

Typically you would append your library path to the include path using set_include_path(). Then, depending on the naming conventions, you would parse the class name to determine it's filepath. The most common would be http://framework.zend.com/manual/en/cod ... tions.html where underscores are converted to classes. I.e.

Code: Select all

$foo = new Foobar_Database_Mysql();
would look for foobar/database/mysql.php relative to your include path.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Best way of __autoload'ing from several folders?

Post by kaisellgren »

Jcart wrote:Typically you would append your library path to the include path using set_include_path(). Then, depending on the naming conventions, you would parse the class name to determine it's filepath. The most common would be http://framework.zend.com/manual/en/cod ... tions.html where underscores are converted to classes. I.e.

Code: Select all

$foo = new Foobar_Database_Mysql();
would look for foobar/database/mysql.php relative to your include path.
Yea that's what I was first going to do.. It's just that the classnames have to be then foobar_database_mysql :P -- at least no name conflicts this way :)
Post Reply