Lazy Loading in PHP with __autoload
__autoload is one of the magic methods added to PHP in version 5. It creates a very easy way for you to manage all your different class files. Actually, with __autoload you don’t need to manage them.
Often you will see in PHP projects files that include the other files needed for that page. You can then end up with a while bunch of require_once calls and so on. This can be quite tedious to maintain and you may not always need all the files.
When you create a new instance of a class PHP checks for it and if it can’t be found the __autoload method is called. It’s passed in one parameter, the name of the class.
-
function __autoload($class){
-
-
require "/path/to/class/files/$class.php";
-
-
}
-
-
$x = new Foo();
Above is a simple example of autoload in action. When a new Foo is created the autoload function requires /path/to/class/files/Foo.php. So with a simple naming convention each class in its own file where the file name is the name of the class (similar to Java) you can easily include files.
This example can be taken slightly further to manage hierarchies of classes…
-
function __autoload($class){
-
-
$path = "/path/to/class/files/$class.php";
-
require $path;
-
-
}
-
-
$x = new Foo_Bar();
The above code simply replaces all underscores ( _ ) with forward slashes ( / ) before requiring the class. The naming convention changes slightly for this, the class name Foo_Bar would be found in following path: /path/to/class/files/Foo/Bar.php.
-
function __autoload($class){
-
-
$path = "/path/to/class/files/$class.php";
-
-
require $path;
-
} else {
-
// Error handling here, log it etc.
-
// Exceptions cannot be thrown.
-
}
-
-
}
-
-
$x = new Foo_Bar_Fail();
The final example shows the same code again with some optional error checking. It allows you to log errors and so on. However, exceptions cannot be thrown by __autoload unless its being called by class_exists. Since this function calls autoload to look for classes. When trying to throw exceptions, PHP runs into a fatal error before its thrown due to the order or __autload. This means a fatal error will be triggered as the class can’t be found and the exception will vanish.
Following up to this post I plan to do one of spl_autoload soon. With this you can have PHP automatically run its own autoload procedure on include paths. For further reading on this follow the link below. The documentation is a little sparse however.
Enjoy, lazy loading!
Useful links;
Tags: lazy loading, Magic Methods, PHP, spl_autoload
April 29th, 2008 at 6:49 pm
Thanks, this really helped. Didn’t know there was anything like autoload <3
April 29th, 2008 at 6:52 pm
No problem, glad to see my new blog is helping some people.
May 9th, 2008 at 12:30 pm
Do you have the code in production somewhere? As far as I know Exceptions doesn’t work inside of __autoload…
May 9th, 2008 at 1:10 pm
Your completely right. I’m sure I had a test case of this working but I must have been tripping out.
Updating it now to reflect that, thanks.
May 20th, 2008 at 11:14 pm
It’s a pity, tough. I rely heavily on autoloading of classes, many times with actually knowing if it can/may happen. Exceptions would have made it much more robust!
May 20th, 2008 at 11:18 pm
Yeah, I completely agree.
There are some work arounds to use class_exists with it, you can see them in the comments in the PHP docs. They are very messy however - but sometimes PHP tends to be like that.