PHP class automatic loading mechanism notes

Source: Internet
Author: User

Before PHP5, each PHP framework, if you want to implement the automatic loading of classes, is generally a kind of convention to implement a traversal directory, automatically load all the files that meet the Convention rules of the class or function. Of course, PHP5 support is not very good before, and the use of classes is not now frequent. After PHP5, when the PHP class is loaded, the Zend engine automatically invokes the __autoload function if the class contains files that are not included, or if the class name fails. This function requires the user to implement the __autoload function himself. After the PHP5.1.2 version, you can use the Spl_autoload_register function to customize the automatic load handler function. When this function is not called, the Spl_autoload function, which is customized by SPL, is used by default. Look at the following two examples:

1, __autoload Example:

The code is as follows Copy Code

function __autoload ($class _name) {
Echo ' __autload class: ', $class _name, ' <br/> ';
}
New Demo ();

The above code will output at the end: __autload Class:demo.
And after this error shows: Fatal error:class ' Demo ' not found

2, spl_autoload_register Example:

The code is as follows Copy Code

function ClassLoader ($class _name) {
Echo ' SPL load class: ', $class _name, ' <br/> ';
}
Spl_autoload_register (' ClassLoader ');
New Demo ();

The above code will output at the end: SPL load Class:demo.
And after this error shows: Fatal error:class ' Demo ' not found

The above two examples show that when a class does not exist (that is, the class is not in the class symbol table), the Zend engine will call again a user-defined function, such as a __autoload or spl_autoload_register registered function. If these two methods exist at the same time, which one will the program call? Or is it called two? Look at one of these examples, what do you think it will output?

The code is as follows Copy Code

function __autoload ($class _name) {
Echo ' __autload class: ', $class _name, ' <br/> ';
}
function ClassLoader ($class _name) {
Echo ' SPL load class: ', $class _name, ' <br/> ';
}
Spl_autoload_register (' ClassLoader ');
New Demo ();

__set, __tostring and other types of magic methods of the constant definition of the source level is together, but it is not a particular class of magic methods. It is the automatic loading magic method shared by all classes. It will exist as a global function. So how does the Zend engine call this method when the class is not found?

Whether you use the New keyword to create an instance of a class, implement an interface using implement, or inherit a class, all of these operations may call the __autoload function. These operations have one thing in common at the source layer, and they need to get information about the class at the time of execution (the interface is essentially a class). They eventually call the Zend_fetch_class (ZEND/ZEND_EXECUTE_API.C) function, which has little content in itself, and the key is that it calls ZEND_LOOKUP_CLASS_EX (Zend/zend_ Execute_api.c function, which is the truth of the automatic loading of the class.

In the ZEND_LOOKUP_CLASS_EX function, we see that the program first queries the class symbol table, if there is a class that returns directly. If it does not exist, it will perform what we call automatic loading. The functions that are related to the __autoload function and SPL are processed here, and functions called function Zend_call_function are passed to the Zend engine by the first and second parameters.

In the Zend_call_function function, it determines whether the second parameter has a function, and if there is a function, only the function passed by the second argument is called (here refers to the function that SPL registers). If the second function has no value, the function passed by the first argument is executed (this refers to the user-defined __autoload function). Here, I think the two methods mentioned above should have the answer, this is a PHP tutorial based on.

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.