Php_autoload automatic loading class and mechanism analysis _ PHP Tutorial

Source: Internet
Author: User
Tags autoload
Php_autoload automatically loads classes and analyzes the mechanism. Before using PHP5, if you need to use a class, you only need to directly use javasderequire to include it. the code for copying test. class. php is as follows :? Phpclassabc {function _ const 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 example test. class. php code is as follows :? Php class abc {function _ const...

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.