Two kinds of PHP automatic loading function

Source: Internet
Author: User

When using the thinkphp framework, it is useful to look at his source code for both __autoload and Apl_autoload_register functions, which are used for automatic loading. Its main role is that when your source file uses a file that has not yet been loaded, the two functions are triggered to load the file that is not loaded.

PHP's __autoload function is a magic function, before this function appears, if a php file refers to 100 objects, then this file will need to use include or require to introduce 100 class files, which will result in the PHP file is extremely large. So we have this __autoload function.

When does the __autoload function call?

When instantiating an object using the New keyword in a php file, if the class is not defined in this PHP file, it will trigger the __autoload function, at which point the php file defining the class can be introduced, and then it can be instantiated successfully. (Note: If you need to instantiate an object, the definition of the class has been found 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 being found!";     }  }  $ani = new Animal ();? >

As 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, and the function introduces the animal.php file;
(3) success of instantiation.
Well, to understand the role of __autoload function, and then look at the role of spl_autoload_register function.
The purpose of the Spl_autoload_register function is to replace the custom function settings with the __autoload function (note: When both __autoload and Spl_autoload_register appear in the file, Spl_autoload _register, whichever is the same)
Then change the main.php to the same effect as:

#main. php<?php  function Myload ($classname) {     $classpath = "{$classname}.php";     if (file_exists ($classpath)) {         require_once ($classpath);     } else{         echo $classpath. "Not being found!";     }  }  Spl_autoload_register ("Myload");  $ani = new Animal ();? >

When using the thinkphp framework, look at his source code useful to __autoload and apl_autoload_register these two functions, and these two functions are used for automatic loading, data source https://blog.csdn.net/ gavin_new/article/details/52805811
Its main role is that when your source file uses a file that has not yet been loaded, the two functions are triggered to load the file that is not loaded.

PHP's __autoload function is a magic function, before this function appears, if a php file refers to 100 objects, then this file will need to use include or require to introduce 100 class files, which will result in the PHP file is extremely large. So we have this __autoload function.

When does the __autoload function call?

When instantiating an object using the New keyword in a php file, if the class is not defined in this PHP file, it will trigger the __autoload function, at which point the php file defining the class can be introduced, and then it can be instantiated successfully. (Note: If you need to instantiate an object, the definition of the class has been found 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 being found!";     }  }  $ani = new Animal ();? >

As 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, and the function introduces the animal.php file;
(3) success of instantiation.
Well, to understand the role of __autoload function, and then look at the role of spl_autoload_register function.
The purpose of the Spl_autoload_register function is to replace the custom function settings with the __autoload function (note: When both __autoload and Spl_autoload_register appear in the file, Spl_autoload _register, whichever is the same)
Then change the main.php to the same effect as:

#main. php<?php  function Myload ($classname) {     $classpath = "{$classname}.php";     if (file_exists ($classpath)) {         require_once ($classpath);     } else{         echo $classpath. "Not being found!";     }  }  Spl_autoload_register ("Myload");  $ani = new Animal ();? >

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.