Recipe 7.21. Autoloading Class Files upon Object Instantiation


7.21.1. Problem

You don't want to include all your class definitions within every page. Instead, you want to dynamically load only the ones necessary in that page.

7.21.2. Solution

Use the __autoload( ) magic method:

function __autoload($class_name) {     include "$class_name.php"; } $person = new Person;

7.21.3. Discussion

When you normally attempt to instantiate a class that's not defined, PHP dies with a fatal error because it can't locate what you're looking for. Therefore, it's typical to load in all the potential classes for a page, regardless of whether they're actually invoked.

This has the side effect of increasing processing time, as PHP must parse every class, even the unused ones. One solution is to load missing code on the fly using the __autoload( ) method, which is invoked when you instantiate undefined classes.

For example, here's how you include all the classes used by your script:

function __autoload($class_name) {     include "$class_name.php"; } $person = new Person;

The __autoload( ) function receives the class name as its single parameter. This example appends a .php extension to that name and tries to include a file based on $class_name. So when you instantiate a new Person, it looks for Person.php in your include_path.

When __autoload( ) fails to successfully load a class definition for the object you're trying to instantiate, PHP fails with a fatal error, just as it does when it can't find a class definition without autoload.

If you adopt the PEAR-style naming convention of placing an underscore between words to reflect the file hierarchy, use the code in Example 7-41.

Autoloading classes using PEAR naming conventions

function __autoload($package_name) {     // split on underscore     $folders = split('_', $package_name);     // rejoin based on directory structure     // use DIRECTORY_SEPARATOR constant to work on all platforms     $path    =  join(DIRECTORY_SEPARATOR, $folders);     // append extension     $path   .= '.php';     include $path; }

With the code in Example 7-41, you can do the following:

$person = new Animals_Person;

If the class isn't defined, Animals_Person gets passed to __autoload( ). The function splits the class name on underscore ( _ ) and joins it on DIRECTORY_SEPARATOR. This turns the string into Animals/Person on Unix machines (and Animals\Person on Windows).

Next, a .php extension is appended, and then the file Animals/Person.php is included for use.

While using __autoload( ) slightly increases processing time during the addition of a class, it is called only once per class. Multiple instances of the same class does not result in multiple calls to __autoload( ).

Before deploying __autoload( ), be sure to benchmark that the overhead of opening, reading, and closing the multiple files necessary isn't actually more of a performance penalty than the additional parsing time of the unused classes.

In particular if you're using an opcode cache, such as APC or Zend Accelerator, using __autoload( ) and include_once can hurt performance. For best results, you should include all your files at the top of the script and make sure you don't reinclude a file twice.

7.21.4. See Also

Documentation on autoloading is available at http://www.php.net/manual/language.oop5.autoload.php.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net