PHP implements the lazy loading method. php implements the loading _ PHP Tutorial

Source: Internet
Author: User
Tags autoload
PHP implements the lazy loading method, and php implements loading. PHP implements the lazy loading method. php implements the loading of this article. this example describes how PHP implements lazy loading. Share it with you for your reference. The specific analysis is as follows: normally, php is loaded by using I PHP to implement lazy loading. php implements loading.

This article describes how PHP implements lazy loading. Share it with you for your reference. The specific analysis is as follows:

Normally, php loads external files by using methods such as include () and require (), and then calls the method through the instance or directly calls the static method, in this case, it is very troublesome to write the import statement. some frameworks will introduce all the files in the specific path and directly instantiate them for use. However, some class packages may not be used in this way, the more class packages you write, the more things you load, which affects the program performance.

Using PHP Reflection class ReflectionClass, you can directly obtain a reflection class of the corresponding class:

The test. php file is as follows:

<?php class test{   public function showName(){     var_dump(__CLASS__);   } }?>

The index. php file is as follows:

<?phpvar_dump(get_included_files()); $rf = new ReflectionClass('test');var_dump(get_included_files());$testObj = $rf->newInstance();$testObj->showName();function __autoload($classname){  $classpath = './' . $classname . '.php';  if (file_exists($classpath)) {    require_once($classpath);  }else {    echo 'class file'.$classpath.'not found!';  }}?>//array// 0 => string 'D:\code\www\test\index.php'(length=26)//array// 0 => string 'D:\code\www\test\index.php'(length=26)// 1 => string 'D:\code\www\text\test.php'(length=25)//string 'test' (length=4)

Instantiate a ReflectionClass and pass the class name in, a reflection class of the corresponding class will be obtained. When you call newInstance by using an instance, you will get an instance of the reflection class. This completes the instantiation.

Through the get_included_files () method, we can see the file introduced on the current page. Before instantiating the reflection class, only index. php file. after the reflection class is instantiated, a test is automatically introduced. php file, then look at the code above and find a magic method named _ autoload (). This method defines automatic file loading. when ReflectionClass cannot find a class on the current page, it will call _ autoload () to load the class. This is the automatic loading process.

To know whether the _ autoload () method is enabled, you can use the method in the PHP Standard Library SPL:

var_dump(spl_autoload_functions());spl_autoload_register('newAutoload');var_dump(spl_autoload_functions());$testObj1 = getInstance('test');$testObj2 = getInstance('test');$testObj3 = getInstance('test');function getInstance($class, $returnInstance = false){  $rf = new ReflectionClass($class);  if ($returnInstance)     return $rf->newInstance();}function newAutoload($classname){  $classpath = './' . $classname . '.php';  if (file_exists($classpath)) {    var_dump('require success');    require_once($classpath);  } else {    echo 'class file ' . $classpath . ' not found!';  }}//array// 0 => string '__autoload' (length=10)//array// 0 => string 'newAutoload' (length=11)//string 'require success' (length=15)

The SQL _autoload_functions () method is used to view the current automatic loading method. Currently, there is a _ autoload magic method. Therefore, the function name is returned. If no automatic loading method is defined, false is returned, the spl_autoload_register () method registers a method to the automatic loading method by using the method name. here, the newAutoload method is used to replace the _ autoload method.

In the newAutoload method, after each successful execution, the 'require success 'is printed only once, indicating that although the instance has been ReflectionClass ('test') three times '), however, because the test class has been loaded once, the automatic loading method will not be executed. Using the getInstance () method to load classes is much more convenient than the previous include () method. you only need to load the file that writes the getInstance () method.

The auto-loading method of rewriting can define different file paths by determining the class name as needed. GetInstance can be used to save the instance with static variables, which also uses the Singleton mode in the design mode.

I hope this article will help you with php programming.

The example in this article describes how PHP implements lazy loading. Share it with you for your reference. The specific analysis is as follows: normally php is loaded through I...

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.