Php_autoload automatic loading class and mechanism analysis
Source: Internet
Author: User
When using the php oo mode to develop a system, we usually get used to storing the implementation of each class in a separate file, which makes it easy to reuse classes, at the same time, it will be very convenient for future maintenance. This is also one of the basic ideas of OO design. before PHP5, if you need to use a class, you only need to directly include it using include/require.
Test. class. php
The code is as follows:
Class abc {
Function _ construct ()
{
Echo 'www .hzhuti.com;
}
}
?>
Load. php
The code is as follows:
The code is as follows:
Class LOAD
{
Static function loadClass ($ class_name)
{
$ Filename = $ class_name. ". class. php ";
If (is_file ($ filename) return include_once $ filename;
}
}
/**
* Set automatic object loading
* Spl_autoload_register-Register given function as _ autoload () implementation
*/
Spl_autoload_register (array ('load', 'loadclass '));
$ A = new Test (); // automatically loads classes. many frameworks use this method to automatically load classes.
?>
_ Autoload ()
In actual projects, it is impossible to write all classes in one PHP file. when you need to call the classes declared in another file in one PHP file, you need to introduce this file through include. However, in many file projects, you need to include all the files of the required classes one by one, a big headache is that you have to write a long list of contained files at the beginning of each class file. Can we import the php file where this class is located when we use any classes?
To this end, PHP provides the _ autoload () method, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.
A parameter received by the _ autoload () method is the class name of the class to be loaded. Therefore, the class name must correspond to the file name, for example, Person. php, the corresponding class name is Pserson.
The following shows a complete instance.
The code is as follows:
Class ClassA {
Public function _ construct (){
Echo "ClassA load success !";
}
}
// Define a class ClassA named ClassA. php
Class ClassA {
Public function _ construct (){
Echo "ClassA load success !";
}
}
Class ClassB extends ClassA {
Public function _ construct (){
// Parent ::__ construct ();
Echo "ClassB load success !";
}
}
// Define a class ClassB. the file name is ClassB. php, and ClassB inherits ClassA
Class ClassB extends ClassA {
Public function _ construct (){
// Parent ::__ construct ();
Echo "ClassB load success !";
}
}
After defining the classes used for testing, we compile a PHP running program file containing the _ autoload () method as follows:
The code is as follows:
Function _ autoload ($ classname ){
$ Classpath = "./". $ classname. '. php ';
If (file_exists ($ classpath )){
Require_once ($ classpath );
}
Else {
Echo 'class file'. $ classpath. 'not found! ';
}
}
$ Newobj = new ClassA ();
$ Newobj = new ClassB ();
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.