Php automatic loading implementation method _ PHP Tutorial

Source: Internet
Author: User
Php is automatically loaded in two ways. Php supports two automatic loading methods. the first scheme uses _ autoload, which is relatively simple and weak. but there is a problem that cannot be solved, that is, to determine whether the file exists before include. there are two methods for automatic loading of the replication on behalf of php.
The first scheme uses _ autoload, which is relatively simple and weak.
But there is a problem that cannot be solved, that is, to determine whether the file exists before include.

The code is as follows:


Set_include_path ('A'. PATH_SEPARATOR. get_include_path ());
Function _ autoload ($ className)
{
// If this check is added, because the file is not in the current directory, it will not detect the file,
// But the include operation is successful.
If (file_exists ($ className. '. php ')){
Include_once ($ className. '. php ');
} Else {
Exit ('No file ');
}
}
$ A = new Acls ();


The second method is to use spl for automatic loading.
Spl_autoload_register ()
A simple example

The code is as follows:


Set_include_path ('A'. 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 () will automatically call spl_autoload () to find ". php "program. the default search extension is ". ini ", you can also use spl_autoload_extenstions () to register an extension.
You can also use your own defined functions to find the missing conditions.
For example
Function loader1 ($ class)
{
// Write some loaded code by yourself
}
Function loader2 ($ class)
{
// When loader1 () cannot be found, I will come
}
Spl_autoload_register ('loader1 ');
Spl_autoload_register ('loader2 ');
More ........
How does the MVC framework implement automatic loading?
First, set the path
'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 the controller and method.
Then set automatic loading

The code is as follows:


Class Loader
{
/**
* Automatically load classes
* @ Param $ class name
*/
Public static function autoload ($ class)
{
$ Path = '';
$ Path = str_replace ('_', '/', $ class). '. php ';
Include_once ($ path );
}
}
/**
* Automatic SQL loading
*/
Spl_autoload_register (array ('loader ', 'autoload '));


Route, instantiate the controller, call the method, and execute what you write.

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

Invalid. the first scheme uses _ autoload, which is relatively simple and weak. but there is a problem that cannot be solved, that is, to determine whether the file exists before 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.