The principle and usage analysis of PHP object-oriented automatic loading mechanism _php skills

Source: Internet
Author: User
Tags autoload phpword yii

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

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

Read a piece of code first

<?php
function __autoload ($classname) {
  $filename = "./". $classname. ". PHP ";
  Include_once ($filename);
New A ();

This example instantiates a class A, but there is no code in the code block of Class A, which is supposed to be an error because no Class A is found, but if you use the AutoLoad () Auto load function, the result can be different

From the above flowchart: in the page instantiate a new class, will first in the current directory to find the corresponding class code, if not to go to the AutoLoad stack to find the corresponding automatic load function, if some words will automatically load the class, no words will throw errors.

This is a mechanism for PHP to load automatically. Then the emphasis is on the back. What 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 future releases.

However, in Phpexecl and Phpword inside all use this function to do automatic loading, but the difference between the two!!

Phpexecl method of automatic loading ( here the author is estimated to be a Python engineer, otherwise the curly braces are not, expressed in 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 complete the redefinition of automatic loading, but are there differences? If you run your code independently, you can run both scenarios, but if you are consolidating into a framework, such as the YII framework. Then the automatic loading of the Phpword is invalid.

Because the YII framework is automatically loaded with an automatic load function, and the code is run, the spl_autoload_register () loads the new auto load function, loading the back of the AutoLoad queue. When all the Phpword are running,

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

So, in turn, look at the phpexecl load function, and you'll see.

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.