Feb 1, 2011

PHP 5.3 Quicktip - __autoload

With PHP 5, true autoloading is possible. No more calling 'include' for every script you have. All you need to do is define an autoload function like so:
function __autoload($class_name)
{
  include $class_name.'.php';
}
Note - This is the simplest example. You can go as far as to create classes for autoloading that will differ depending on the needed directory structure.

Now when you call classes, if they weren't included, PHP will look for them and call that function to load them.

No comments:

Post a Comment