how do i autoload classes?

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
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

how do i autoload classes?

Post by pedrotuga »

Ok, the title, might not be 100% accurate. This is what i want to do:

I have my application which is working fine, but then, i want to be able to extend it by simply dropping a file with a class in a certain folder.

For example, lets say i have an online stores selling books, CDs and DVDs. Each of these sections would have its own class with some specific functionality and, if i wanted to add a section, say videogames, i could just drop a file containing it.

What i am looking for is sort an equivalent (yet not only limited to that) to Java interfaces.

I thought about a few ways this could be possibly achieved, but i want it to be properly made. I believe this is done out there by many so feel free to point an example.

Elegant/light/neat solutions are welcome rather than complex/fancy/advanced ones.

Thans
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how do i autoload classes?

Post by requinix »

Being able to just "drop a file" into the system and have it react appropriately requires lots of planning and foresight. OOD can help but you'll have to do a lot of thinking and researching to get something as powerful as what you're describing.

PHP 5 has does interfaces, you know.

For the record, I'm not talking about __autoload. I'm talking about the whole "I add this Videogames class and suddenly there's a videogames section on the site". __autoload is awesome and makes a lot of things much easier.
Last edited by requinix on Fri Oct 09, 2009 12:41 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how do i autoload classes?

Post by John Cartwright »

Generally your application would specify the folders (your include paths) you want PHP to search for when initializing an object, and based on your naming scheme, your auto loader would attempt to locate the file. The process would look something like:

Defining your application's autoloader. Now this is where your naming scheme would come into play. Lets use the Zend Framework as an example. The class name "Zend_Config", would be translated to Zend/Config.php.

Code: Select all

function __autoload($class) {
    $class= str_replace('_', '/', $class);
    require_once $class_name . '.php';
}
Setting the appropriate include path(s), this is usually done in your applications bootstrap. This is telling PHP which folders you want to attempt it to look at the included files relative to include paths.

Code: Select all

set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/lib');
Now, when we initialize the object in this example:

Code: Select all

$config = new Zend_Config();
our registered autoloader will then look at the following /path/to/lib/Zend/Config.php and attempt to load it
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Re: how do i autoload classes?

Post by pedrotuga »

So if i got it right.... i define my naming pattern inside __autoload(), get the contents of a folder with scandir(), do whatever validations and then loop through the contents and call the constructors.

Is there any constraint calling class Constructors? Particulary, can I call them as a variable name function?
Post Reply