PHP automatic loading mechanism definition and usage examples

Source: Internet
Author: User
Tags autoload phpword yii
This article mainly introduces the principle and usage of PHP object-oriented automatic loading mechanism, analyzes the principle of PHP object-oriented automatic loading mechanism, the related functions and matters needing attention in the case form, and needs friends to refer to

This paper describes the principle and usage of PHP object-oriented automatic loading mechanism. Share to everyone for your reference, as follows:

In the study of the object-oriented PHP, you will know a lot of "grammatical sugar", that is, magic method. There is a magic method that adds automatic loading, called: AutoLoad ();

Look at a piece of code first

<?phpfunction AutoLoad ($classname) {  $filename = "./". $classname. ". PHP ";  Include_once ($filename);} New A ();

The Class A is instantiated here, but there is no related code for Class A in the code block, and it is common sense that the error should be made because no class A is found, but if you use the AutoLoad () auto-load function, the result can be different

From the flowchart above: in the page instantiation of a new class, will first find the corresponding class code in the current directory, if not go to the autoload stack to find the corresponding auto-loading function, if there is a word to automatically load the class, no words will throw an error.

This is a mechanism for PHP to load automatically. Then focus on the back. What to do if I have multiple functions that are automatically loaded!

PHP provides an SPL function

Spl_autoload_register (); Registering the AutoLoad function

Official: Spl_autoload_register () provides a more flexible way to implement automatic loading of classes. Therefore, it is no longer recommended to use the AutoLoad () function, which may be deprecated in later versions.

In Phpexecl and Phpword, however, this function is used to do the automatic loading, but there is a difference!!

Phpexecl Automatic Loading method (the author is estimated to be a Python engineer, otherwise curly braces are not used, which is indicated by indentation )

public static function Register () {    $functions = spl_autoload_functions ();    foreach ($functions as $function)      spl_autoload_unregister ($function);    $functions = Array_merge (Array (' Phpexcel_autoloader ', ' Load '), $functions);    foreach ($functions as $function)      $x = Spl_autoload_register ($function);    return $x;}

Phpword method of automatic loading

public static function Register () {  return Spl_autoload_register (Array (' Phpword_autoloader ', ' Load '));}

Both of these methods can be done to redefine automatic loading, but is there a difference? If you are running code independently, both scenarios can be run, but if you integrate into the framework, such as the YII framework. Then the automatic loading of the Phpword is invalid.

Because the YII framework automatically comes with an auto-load function, and it is already registered when the code is running, Spl_autoload_register () loads the new auto-load function after the autoload queue. All Phpword at the time of operation

It is called the automatic loading mechanism defined by the YII framework, and is not phpword this way of loading.

So instead of looking at PHPEXECL's load function, you'll understand.

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.