Home >Backend Development >PHP Tutorial >How does PHP automatically load the two functions __autoload and apl_autoload_register?

How does PHP automatically load the two functions __autoload and apl_autoload_register?

jacklove
jackloveOriginal
2018-05-22 14:21:591396browse

This article will introduce how to automatically load the two functions __autoload and apl_autoload_register through php?

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.

The main function is, When your source code file uses a file that has not been loaded, these two functions will be triggered to load the file that has not been loaded.

php's __autoload function is a magic function. Before this function appears, If a PHP file references 100 objects, then the file needs to use include or require to introduce 100 class files, 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 the definition of the class in this file, 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();
?--> 
如上述两个文件,运行php main.php
(1)运行到new Animal()时,发现 class Animal没有定义;
(2)触发了__autoload函数,该函数引进了Animal.php文件;
(3)实例化成功。
好了,了解完了__autoload函数的作用,再来看看spl_autoload_register函数的作用。
spl_autoload_register函数的作用就是将自定义函数设置替换为__autoload函数(注意:当文件中同时出现__autoload和spl_autoload_register时,以spl_autoload_register为准)
那么将main.php改成如下也有同样的作用:
#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();
?-->

Introduction to this article How to automatically load the two functions __autoload and apl_autoload_register through php. For more related content, please pay attention to the php Chinese website.

Related recommendations:

php error handling and implementation methods

PHP gets the value specified in the multi-dimensional array in the array How many columns?

Basic learning of php: PHP arrays and data structures

The above is the detailed content of How does PHP automatically load the two functions __autoload and apl_autoload_register?. 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