Section 12th -- Automatic loading of classes -- ClassesandObjectsinPHP5 [12]. Section 12th -- Automatic loading of classes when you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all
Section 12th -- Automatic loading of classes
When you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all, you know which class to use. however, PHP provides the automatic class loading function, which can save programming time. when you try to use a class that is not organized by PHP, it will look for a global function of _ autoload. if this function exists, PHP will use a parameter to call it. the parameter is the name of the class.
Example 6.15 illustrates how _ autoload is used. it assumes that each file in the current directory corresponds to a class. when the script tries to generate a User-like instance, PHP will execute _ autoload. the script assumes that class_User.php defines the User class .. PHP returns the lower-case name no matter whether it is in upper or lower case.
Listing 6.15 Class autoloading
name = "Leon"; $u->printName(); ?>
Lifecycle 12th-automatic loading of classes when you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all...