PHP Automatic loading of two implementation methods, the need for friends can refer to the next.
There are two ways to load PHP automatically.
The first scheme uses AutoLoad, the function is simpler and weaker.
However, one problem is not solved, that is, before the include to determine the existence of the problem of the file.
Set_include_path (' AA '). Path_separator. Get_include_path ()); function AutoLoad ($className) {//If this detection is added, because this file is not in the current directory, it will not detect the existence of the file,//But the include is successful if (File_exists ($className. '. php ') {include_once ($className. '. php '); } else {exit (' no file ');}} $a = new Acls ();
The second scenario is automatically loaded with SPL, which is specifically stated here.
Spl_autoload_register ()
A simple example
Set_include_path (' AA '). Path_separator. Get_include_path ()); function AutoLoad ($className)//{//if (File_exists ($className. '. php ')} {//Include_once ($className. '. php '); } else {//exit (' no file ');//}//} spl_autoload_register (); $a = new Acls ();
Spl_autoload_register () automatically calls Spl_autoload () in the path to find the ". PHP" program with a lowercase file name. The default lookup extension also has ". Ini" and can also be used with the Spl_autoload_ Extenstions () registers the extension.
You can also find a function by defining your own functions in the case of a clear condition that cannot be found.
Such as
function Loader1 ($class)
{
Write some code to load yourself
}
function Loader2 ($class)
{
When Loader1 () was not found, I came to find
}
Spl_autoload_register (' Loader1 ');
Spl_autoload_register (' Loader2 ');
You can have more ...
How the MVC framework implements automatic loading
Set the path First
' Include ' = = Array (' application/catalog/controllers ', ' application/catalog/models ',), $include = Array (' Application/controllers ', ' application/models ', ' application/library ');
Set_include_path (Get_include_path (). Path_separator. Implode (Path_separator, $config [' include ']);
After obtaining the URL, parse out the controller and method.
Then set the auto load
The code is as follows:
Class Loader {/** * automatically loads classes * @param $class class name */public static function AutoLoad ($class) {$path = '; $path = Str_replace (' _ ', '/', $class). '. php '; Include_once ($path); }}/** * SQL Auto Load */spl_autoload_register (Array (' Loader ', ' autoload '));
Route, instantiate the controller, call the method, and the thing you write begins to execute.
The code is as follows:
/** * * * route */Public Function route () {if (Class_exists ($this->getcontroller ())) {$RC = new Reflectionclass ($this->g Etcontroller ()); if ($RC->hasmethod ($this->getaction ())) {$controller = $RC->newinstance (); $method = $RC->getmethod ($ This->getaction ()); $method->invoke ($controller); } else throw new Exception (' no action '); } else throw new Exception (' no controller '); }
The initial automatic loading is complete.