Section 12 - Automatic Loading of Class - Classes and Objects in PHP5 [12]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / 12 - Automatic loading When you try to use an undefined class, PHP will report a fatal error. Solution is to add a class, you can include a file with include. After all, you know which class to use. However, PHP provides the automatic load function of the class. This saves programming time. When you try to use a PHP unmelated class, it will look for __autoload global functions. If this function is present, PHP will use a parameter to call it, parameter is the name of the class. Example 6.15 illustrates how __autoload is used. It assumes that each file is corresponding to a class in the current directory. When the script tries to generate a class user instance, PHP will execute __autoload. Script assumes that Class_User.php defines a User class .. Regardless of the call or lowercase, PHP will return to the name. LLISTING 6.15 Class AutoLoading Php // define autoloading function function __autoload ($ class) {include ("class_". Ucfirst ($ Class). ". PHP ");} // use a class thing Must Be AutoAded $ u = new user; $ u-> name =" leon "; $ u-> printname ();?>>>