__autoload()

__autoload () function automatically loads the class file

The magic method is used to automatically load a program that uses the class file of PHP source file, thus avoiding us go one by one automatically include or require, and will attempt to use this function is called automatically when the class has not yet been defined. By calling this function the scripting engine failure before PHP error had one last chance to load the required class, but I recently read an article, it says __autoload () is not very efficient

  First you need to create a class name the same php class file in the same directory below

  then

    function __autoload($className){
       $file = $className.".php";
       include $file;
    }
    $className = new className;

Note that this function if you need to load properly, you need to file name is set to the class name (recommended lowercase) .class.php such as the class name and the file name can have some connection to the other if this function is abnormal Catch can not be captured, displayed Fatal Error

Guess you like

Origin www.cnblogs.com/starshine-zhp/p/12175425.html