Home  >  Article  >  Backend Development  >  Detailed explanation of two PHP automatic loading functions

Detailed explanation of two PHP automatic loading functions

零到壹度
零到壹度Original
2018-04-11 13:34:332068browse

When using the ThinkPHP framework, viewing its source code is useful for the two functions __autoload and apl_autoload_register, and these two functions are used for automatic loading. Its main function is that when your source code file uses a file that has not been loaded, these two functions will be triggered to load the unloaded file.

php's __autoload function is a magic function. Before this function appeared, if 100 objects were referenced in a PHP file, then the file would need to use include or require to introduce 100 class files, which would cause the PHP file to be extremely large. So there is this __autoload function.

When is the __autoload function called?

When the new keyword is used in the PHP file to instantiate an object, if the class is not defined in the PHP file, the __autoload function will be triggered. At this time, the definition of the class can be introduced. php file, and then it can be instantiated successfully. (Note: If the object that needs to be instantiated has been found in this file and the definition of the class has been found, the __autoload function will not be triggered)

#Animal.php<?php
   class Animal{}?>
#main.php<?php
  function __autoload($classname){
     $classpath = "{$classname}.php";     if(file_exists($classpath)){         require_once($classpath);
     }else{         echo $classpath." not be found!";
     }
  }  $ani = new Animal();?>

Like the above two files, run php main.php
(1) When running to new Animal(), it is found that class Animal is not defined;
(2) The __autoload function is triggered, which introduces the Animal.php file;
(3) The instantiation is successful.
Okay, after understanding the role of the __autoload function, let’s take a look at the role of the spl_autoload_register function.
The role of the spl_autoload_register function is to replace the custom function settings with the __autoload function (note: when __autoload and spl_autoload_register appear in the file at the same time, spl_autoload_register shall prevail)
Then changing main.php to the following will have the same effect Function:

#main.php<?php
  function myLoad($classname){
     $classpath = "{$classname}.php";     if(file_exists($classpath)){         require_once($classpath);
     }else{         echo $classpath." not be found!";
     }
  }
  spl_autoload_register("myLoad");  $ani = new Animal();?>

               

When using the ThinkPHP framework, it is useful to check its source code to use the two functions __autoload and apl_autoload_register, and these two functions are used for automatic loading. Data source https://blog.csdn.net/gavin_new/article/details/52805811
Its main function is that when your source code file uses a file that has not been loaded, these two functions will be triggered to load it. This unloaded file

php's __autoload function is a magic function. Before this function appears, if a php file references 100 objects, then this file needs to use include or require to introduce 100 objects. class file, which will cause the php file to be extremely large. So there is this __autoload function.

When is the __autoload function called?

When the new keyword is used in the PHP file to instantiate an object, if the class is not defined in the PHP file, the __autoload function will be triggered. At this time, the definition of the class can be introduced. php file, and then it can be instantiated successfully. (Note: If the object that needs to be instantiated has been found in this file and the definition of the class has been found, the __autoload function will not be triggered)

#Animal.php<?php
   class Animal{}?>
#main.php<?php
  function __autoload($classname){
     $classpath = "{$classname}.php";     if(file_exists($classpath)){         require_once($classpath);
     }else{         echo $classpath." not be found!";
     }
  }  $ani = new Animal();?>

Like the above two files, run php main.php
(1) When running to new Animal(), it is found that class Animal is not defined;
(2) The __autoload function is triggered, which introduces the Animal.php file;
(3) The instantiation is successful.
Okay, after understanding the role of the __autoload function, let’s take a look at the role of the spl_autoload_register function.
The role of the spl_autoload_register function is to replace the custom function settings with the __autoload function (note: when __autoload and spl_autoload_register appear in the file at the same time, spl_autoload_register shall prevail)
Then changing main.php to the following will have the same effect Role:

#main.php<?php
  function myLoad($classname){
     $classpath = "{$classname}.php";     if(file_exists($classpath)){         require_once($classpath);
     }else{         echo $classpath." not be found!";
     }
  }
  spl_autoload_register("myLoad");  $ani = new Animal();?>

The above is the detailed content of Detailed explanation of two PHP automatic loading functions. For more information, please follow other related articles on the PHP Chinese website!

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