Analysis on the principle and usage of PHP object-oriented automatic loading mechanism, and php object-oriented

Source: Internet
Author: User
Tags phpword

Analysis on the principle and usage of PHP object-oriented automatic loading mechanism, and php object-oriented

This article describes the principles and usage of the PHP object-oriented automatic loading mechanism. We will share this with you for your reference. The details are as follows:

When learning PHP object-oriented, you will know a lot of "syntactic sugar", that is, the magic method. There is a magic method to add automatic loading, called __autoload ();

First read a piece of code

<?phpfunction __autoload($classname) {  $filename = "./". $classname .".php";  include_once($filename);}new a();

A Class A is instantiated here, but there is no code related to Class A in the code block. It is common sense that an error should be reported because the corresponding class A is not found, however, if you use autoload () to automatically load functions, the results will be different.

From the flowchart above: if a new class is instantiated on the page, the corresponding class code is first found in the current directory. If not, the corresponding automatic loading function is found in the autoload stack, if yes, the class is automatically loaded. If no, an error is thrown.

This is a mechanism for PHP to automatically load data. Then focus on the following.What if I have multiple auto-loaded functions!

PHP provides a SPL Function

Spl_autoload_register (); // register the autoload Function

Official: spl_autoload_register () provides a more flexible way to automatically load classes. Therefore, it is no longer recommended to use the _ autoload () function, which may be discarded in future versions.

However, PHPexecl and PHPWord both use this function for automatic loading, but there is a difference between the two !!

PHPexecl automatic loading method (the author estimates it is a Python engineer here, otherwise there will be no curly brackets, expressed in indentation)

public static function Register() {    $functions = spl_autoload_functions();    foreach ( $functions as $function)      spl_autoload_unregister($function);    $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions);    foreach ( $functions as $function)      $x = spl_autoload_register($function);    return $x;}

Automatic PHPWord Loading Method

public static function Register() {  return spl_autoload_register(array('PHPWord_Autoloader', 'Load'));}

The two methods can be used to automatically load the redefinition, but what is the difference? If the code is run independently, both cases can be run, but it should be integrated into the framework, such as the YII framework. The automatic loading of PHPWord is invalid.

Because the YII framework automatically carries an automatic loading function and is registered when the code is run again, spl_autoload_register () will load the new automatic loading function to the end of the autoload queue. When all phpwords are running

The automatic loading mechanism defined by the YII framework is called, and it is not the PHPWord loading method.

So you will understand the Loading Function of PHPexecl in turn.

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.