Php magic method _ autoload ()

Source: Internet
Author: User
Php magic method _ autoload ()

Php magic method _ autoload (). If you need it, refer to it.


The _ autoload () method is a special function. it is not a class method, but a separate function and is declared outside the class, it will be called in the instantiation of a class that has not been declared.
Example:

  1. Require_once ('test/A. php ');
  2. Require_once ('test/B. php ');
  3. Require_once ('test/C. php ');
  4. If (condition ){
  5. $ A = new ();
  6. $ B = new B ();
  7. $ C = new C ();
  8. } Else if (condition B ){
  9. $ A = newA ();
  10. $ B = new B ();
  11. }

In this case, A problem occurs. when class B is instantiated under Condition B, the and C files do not need to be referenced. Therefore, some resources are wasted to compile class, C two "useless classes", so we can use the _ autoload () function to solve this problem.

  1. Function _ autoload ($ className ){
  2. $ FilePath = "test/{$ className}. php ";
  3. If (is_readable ($ filePath )){
  4. Require ($ filePath );
  5. }
  6. }
  7. If (condition ){
  8. $ A = new ();
  9. $ B = new B ();
  10. $ C = new C ();
  11. } Else if (condition B ){
  12. $ A = newA ();
  13. $ B = new B ();
  14. }

When the php engine uses Class A for the first time but cannot find it, the _ autoload method is automatically called and the class name "A" is passed in as A parameter. So what we need to do is find the corresponding file based on the class name and include it. if our method cannot be found, the php engine will report an error. Note: only require can be used here, because once included, when the php engine encounters Class A, it will not call _ autoload, but directly use Class A in the memory, does not cause multiple inclusion.

Now let's talk about the running mechanism of autoload. When PHP instantiates an object (in fact, this is the case when the interface is implemented, using static variables in a class constant or class, and calling static methods in the class ), first, the system checks whether the class (or interface) exists. if it does not exist, it tries to use the autoload mechanism to load the class. The main execution process of the autoload mechanism is:

(1) Check whether the global variable function pointer autoload_func of the executor is NULL.
(2) if autoload_func = NULL, check whether the _ autoload () function is defined in the system. if not, report an error and exit.
(3) If the _ autoload () function is defined, execute _ autoload () to load the class and return the loading result.
(4) If autoload_func is not NULL, the autoload_func pointer is directly executed to load the class. In this case, the _ autoload () function is not defined.

Php

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.