Two ways to implement PHP automatic loading _php tutorial

Source: Internet
Author: User
Tags autoload
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 ...

  • 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.