PHP - Stop including class files and use __autoload() instead
PHP added several magic methods in PHP5. __autoload(), however, isn’t one of them. But that doesn’t make it any less useful. In fact it’s one of the gems in PHP that I find to be relatively under used. It’s common for PHP applications to break out classes into their own files. This becomes cumbersome when working on large projects as you wind up with numerous include/require calls for any given page. There’s got to be a better way...
Consistency is your friend
I’m sure you name your class files consistently so you can probably
skip this section. Apparently, since you’re reading this, you do not
have any rhyme or reason for your class file names. It doesn’t really
matter what it is as long as it’s consistent and predictable. For
example, EpiCode contains a models
directory which contains all of the PHP class files. The file names
follow the pattern ClassName.php. I know that class A is defined in models/A.php.
What can __autoload() do for you?
Did you know that PHP will call __autoload() if you try to call a
function which is not yet defined? You simply have to define it and
let it know where to find the class file. Let’s use my example of
placing all class definitions inside a models directory with the filename being the same as the class name. Your __autoload() function may look something like this.
function __autoload($className)
{
require_once "./models/{$className}.php";
}
// Instantiate class A without including it and __autoload() will do so on your behalf
$ClassA = new A();
Make your code less ugly
If you can’t spare an extra function call here or there then
__autoload() may not be for you. Though I would begin to question your
reasoning. The upside is that your code could become significantly
cleaner and more maintainable. The upside of easy to read code often
trumps everything else.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Matt Farina replied on Tue, 2008/07/08 - 10:08am
Daniel Oosterhuis replied on Tue, 2008/08/05 - 5:06am
It would have been useful to mention probably the most common naming convention for classes , as done with PEAR classes. In your example, you assume that all of your classes reside in one directory.
For large applications I find it very useful to reflect the class hierarchy in your filesystem. For example:
would be reflected in your filesystem as:
/models
/A
Special.php
A.php
Your autoloader would then look something like
function __autoload($class_name) {$path = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
require_once($path);
}
where DIRECTORY_SEPERATOR is a global constant provided by PHP (a forward slash / for *NIX systems).
This will especially payoff when working with a subversioning system or when you want to release your classes in packages.
Mike Seth replied on Tue, 2008/08/26 - 4:39am
suresh rajagopal replied on Thu, 2009/02/19 - 12:12am