Section 12 - Automatic Loading of Class 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, do you know which class you want to use? However, PHP provides automatic load capabilities, which saves programming. When you try to use a PHP unmelable class, it will look for a global function of __autoload. If this function is present, PHP will use one The parameter calls it, the parameter is the name of the class. Example 6.15 illustrates how __autoload is used. It assumes that each file corresponds to a class in each file. When the script is trying to generate an instance of a class user, PHP will execute __autoload Script assumes that there is a User class in class_user.php .. No matter whether it is called or lowercase, PHP will return to the name. LLISTING 6.15 Class Autolaoading
// Define autoload function function __autoload ($ class) {include ("class_". Ucfirst ($ class). ".php");} // use a class thing must be autoloaded $ u = new user; $ u-> Name = "leon"; $ u-> printname ();?>