[Previous] [Contents]


Autoloading

An additional feature in PHP provides for automatic loading of include files which I'll make quick mention of here in case you run across code that use it.

Since most of my class definitions will be in separate files that I can reuse and share between web application scripts, I are obliged to require or include those files in any script that uses those classes.

PHP5 provides the ability to skip these inclusions, if you so desire, by letting you implement a special function in your script called __autoload. This function receives the name of a class for which PHP cannot find a definition as a parameter. Therefore, you can determine which file you wish to include inside of this function, and then issue the appropriate require or include (or require_once or include_once) operation.

<?php

  //
  // all my classes are in files with the pattern:
  // "classname".inc
  //
  function __autoload($in_className)
  {
    require_once("$in_className.inc");
  }

?>

Again, I'll not use this functionality in the tutorial since I'll always strive to be explicit and deliberate in my coding. Also, this functionality will not work if my autoloaded class inherits from a base class that is in a separate file that has not been loaded.

[Previous] [Contents]