What is the relationship between the spl_autoload () function and spl_autoload_register?

Source: Internet
Author: User
Tags abstract definition autoload spl
When learning the php autoload mechanism, I found that all classes are automatically loaded using the spl_autoload_register () function. However, I found the spl_autload () in the official php manual, this function is interpreted as __autoload () function... when learning the php autoload mechanism, I found that all classes are automatically loaded using the spl_autoload_register () function. However, I found the spl_autload () this function is explained in the official php manual as follows:

__autoload()Default function implementation

In the example of this function, we can see that the function is similar to include (), except that the second parameter can be passed in to set the suffix name. In addition, spl_autoload () cannot specify the path (of course, you can call set_include_path () to specify the path before calling this function)
In addition, I saw some blogs saying:

How can we make spl_autoload automatically take effect, that is, direct autoload_func to spl_autoload? The answer is to use the spl_autoload_register function. If you do not use any parameters when calling spl_autoload_register () for the first time in a PHP script, you can direct autoload_func to spl_autoload.

However, in my own test, I found that only spl_autoload () is called, and the class name to be loaded is passed in, so that the class can be directly loaded without calling spl_autoload_register ().

My problem is:

  1. In the PHP manual, how does one understand the default implementation of the __autoload () function?

  2. What is the relationship between spl_autoload () and spl_autoload_register?

  3. What does the spl_autoload () function mean?

Thank you!

Reply content:

When learning the php autoload mechanism, I found that all classes are automatically loaded using the spl_autoload_register () function. However, I found the spl_autload () this function is explained in the official php manual as follows:

__autoload()Default function implementation

In the example of this function, we can see that the function is similar to include (), except that the second parameter can be passed in to set the suffix name. In addition, spl_autoload () cannot specify the path (of course, you can call set_include_path () to specify the path before calling this function)
In addition, I saw some blogs saying:

How can we make spl_autoload automatically take effect, that is, direct autoload_func to spl_autoload? The answer is to use the spl_autoload_register function. If you do not use any parameters when calling spl_autoload_register () for the first time in a PHP script, you can direct autoload_func to spl_autoload.

However, in my own test, I found that only spl_autoload () is called, and the class name to be loaded is passed in, so that the class can be directly loaded without calling spl_autoload_register ().

My problem is:

  1. In the PHP manual, how does one understand the default implementation of the __autoload () function?

  2. What is the relationship between spl_autoload () and spl_autoload_register?

  3. What does the spl_autoload () function mean?

Thank you!

The _ autoload function is used to process automatically loaded functions. When PHP cannot find the specified class, it will call the automatically loaded class to load the required class.
_ Autoload is just an abstract definition. Implementation (implementation is to define how to load, what the loading rules are, what the loaded files are, and so on) is handed over to the user, spl_autoload is an implementation of _ autoload defined by SPL. The load rule implemented by the spl_autoload function is to find the appropriate class in include paths. Spl_autoload follows a load rule for the psr-0, and include paths is the path to be queried during loading.
Other self-implemented _ autoload classes can be registered through spl_autoload_register. After registration, you can automatically call the registered method to load the class as needed. Spl_autoload is also an implementation of _ autoload, which also needs to be registered on a regular basis, but because it is an internal default implementation, all of them have been automatically registered in PHP.
Spl_autoload is not very useful nowadays. It should be because historical problems are left in PHP. Currently, most programs do not use spl_autoload for automatic loading because its rules are no longer fixed, it is not suitable for derivative functions.

Insert a sentence. Because PHP only has one automatic loading method, SPL's spl_autoload and spl_autoload_register need to compete for this method. Therefore, many compromise methods are used in SPL's C implementation. When you do not use spl_autoload_register to register any custom auto-loading functions, the PHP auto-loading method is attached to spl_autoload, and after spl_autoload_register registers the auto-loading function, the Automatic Loading Method of PHP is attached to the spl_autoload_call method, and spl_autoload will become a standby option to enter the automatic loading queue of spl_autoload_register.

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.