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.
Copy CodeThe code is as follows:
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 can be 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
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
Class Loader
{
/**
* Auto Load Class
* @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.
Copy CodeThe code is as follows:
/**
* Routing
*/
Public Function route ()
{
if (Class_exists ($this->getcontroller ())) {
$RC = new Reflectionclass ($this->getcontroller ());
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.
http://www.bkjia.com/PHPjc/322152.html www.bkjia.com true http://www.bkjia.com/PHPjc/322152.html techarticle There are two ways to load PHP automatically. The first scheme uses __autoload, which is simpler and weaker. But there is one problem that is not resolved, that is, the question of whether the file exists before the include. Copy generation ...