Home  >  Article  >  Backend Development  >  php magic method __autoload()

php magic method __autoload()

WBOY
WBOYOriginal
2016-07-25 08:46:22847browse

php magic method __autoload(), friends in need can refer to it.


__autoload() method is a special function. It is not a class method, but a separate function. It is declared outside the class and will be called when instantiating a class that has not been declared yet.
To give a chestnut:

  1. require_once('test/A.php');
  2. require_once('test/B.php');
  3. require_once('test/C.php');
  4. if (condition A) {
  5. $a = new A();
  6. $b = new B();
  7. $c = new C();
  8. } else if (condition B) {
  9. $a = newA();
  10. $b = new B();
  11. }
Copy code

A problem will arise when writing like this. When class B is instantiated under condition B, there is actually no need to reference files A and C. Therefore, the method in the chestnut will waste some resources to compile two "useless files" A and C. "class"; so at this time 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 A) {
  8. $a = new A();
  9. $b = new B();
  10. $c = new C();
  11. } else if (Condition B) {
  12. $a = newA();
  13. $b = new B();
  14. }
Copy code

When the php engine uses class A for the first time but cannot find it, it will automatically call the __autoload method and pass in the class name "A" as a parameter. Therefore, what we need to do is to find the corresponding file according to the class name and include it. If our method cannot find it, then the PHP engine will report an error. Note that you can only use require here, because once it is included, when the PHP engine encounters class A again, it will not call __autoload, but directly use class A in the memory, which will not cause multiple inclusions.

Now let’s talk about the operating mechanism of autoload. When PHP instantiates an object (actually when implementing an interface, using class constants or static variables in a class, or calling static methods in a class), it will first search in the system. Whether the class (or interface) exists, if not, try to use the autoload mechanism to load the class. The main execution process of the autoload mechanism is:

(1) Check whether the executor global variable function pointer autoload_func 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 try to load the class and return the loading result.
(4) If autoload_func is not NULL, directly execute the function pointed to by the autoload_func pointer to load the class. At this time, it does not check whether the __autoload() function is defined.

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php cookie&session notesNext article:php cookie&session notes