There are two ways to load PHP automatically.

Source: Internet
Author: User
Tags autoload

However, one problem is not solved, that is, before the include to determine the existence of the problem of the file.

12345678910111213 set_include_path(‘aa‘. PATH_SEPARATOR . get_include_path());function__autoload($className){    //如果加这个检测, 因为此文件不在当前目录下,它就会检测不到文件存在,    //但include是能成功的    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

12345678910111213 set_include_path ( ' AA '  < Code class= "PHP plain". 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

123456789101112131415161718 classLoader{    /**     * 自动加载类     * @param $class 类名     */    publicstaticfunctionautoload($class)    {        $path= ‘‘;            $path = str_replace(‘_‘, ‘/‘, $class) . ‘.php‘;        include_once($path);    }} /** * sql自动加载 */spl_autoload_register(array(‘Loader‘, ‘autoload‘));
Route, instantiate the controller, call the method, and the thing you write begins to execute.
12345678910111213141516 /** * 路由 */publicfunctionroute(){    if(class_exists($this->getController())) {        $rc= newReflectionClass($this->getController());        if($rc->hasMethod($this->getAction())) {            $controller= $rc->newInstance();            $method= $rc->getMethod($this->getAction());            $method->invoke($controller);        } else            thrownewException(‘no action‘);    } else        thrownewException(‘no controller‘);}
1
1 初步的自动加载就完成了

There are two ways to load PHP automatically.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.