How to implement lazy loading in PHP

Source: Internet
Author: User
Tags php class php file reflection require

This article mainly introduced the PHP implementation lazy loading method, the example analyzes the PHP load principle and the lazy load realization skill, has certain reference value, the need friend may refer to under

-->

This article describes the method of PHP implementation lazy loading. Share to everyone for your reference. The specific analysis is as follows:

Ordinary PHP loading is through the include (), require () and other methods to load the external file, and then invoke the method through the instance or call the static method directly, and this way to write the introduction of the statement is very cumbersome, and some of the framework will be a specific path of the file into the direct instantiation can be But this kind of package does not necessarily use, write the class pack the more time, the load of things on a lot of, affect the performance of the program.

A reflection class of the corresponding class can be directly obtained through the reflection class Reflectionclass of PHP:

test.php files are as follows:

?
1 2 3 4 5 6 7 <?php class test{Public Function ShowName () {var_dump (__class__);}}?>

index.php files are as follows:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 <?php Var_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:codewwwtestindex.php ' (length=26)//array//0 => string ' d:codewwwtestindex.php ' (len GTH=26)//1 => string ' d:codewwwtexttest.php ' (length=25)//string ' test ' (length=4)

When a reflectionclass is instantiated and the class name is passed in, a reflection class of the corresponding class is obtained. Invoking newinstance with an instance will get an instance of the reflection class, which completes the instantiation.

With the Get_included_files () method, we can see the files introduced by the current page. Before the reflection class is instantiated, only the index.php file, after instantiating the reflection class, automatically introduces a test.php file, then look at the above code and find a magic method for the __autoload () name, which defines the automatic loading file. When Reflectionclass cannot find a class on the current page, it calls __autoload () to load the class. This is the process of automatic loading.

To see if the __autoload () method is open, you can view it through the methods in the standard library SPL of PHP:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Var_dump (Spl_autoload_functions ()); Spl_autoload_register (' Newautoload '); Var_dump (Spl_autoload_functions ()); $TESTOBJ 1 = getinstance (' test '); $TESTOBJ 2 = getinstance (' test '); $TESTOBJ 3 = getinstance (' test '); function getinstance ($class, $returnInstance = False) {$RF = new Reflectionclass ($class); if ($returnInstance) return $RF- >newinstance (); The function newautoload ($classname) {$classpath = './'. $classname. '. php '; if (file_exists ($classpath)) {Var_dump (' require success '); require_once ($classpath);} else {echo ' class file '. $class Path. ' Not found! '; }//array//0 => string ' __autoload ' (length=10)//array//0 => string ' newautoload ' (length=11)//string ' Requi Re success ' (length=15)

The Sql_autoload_functions () method is used to view the current automatic loading method, there is currently a __autoload magic method, so return the function name, if not defined automatic loading method, the return is false, and Spl_autoload_ The register () method is registered to the automatic load method by means of a method name, where the Newautoload method is used to replace the __autoload method.

Newautoload method, each time the execution succeeds, print a ' require success ', which is only printed once, stating that although the instance was 3 times reflectionclass (' test '), but since the test class had been loaded once, The automatic loading method is no longer performed. by getinstance () This method of loading a class is much more convenient than the previous include (), it is only necessary to load the file that writes the getinstance () method.

The overridden automatic loading method can define different file paths, as needed, by determining the name of the class. GetInstance can save an instance with a static variable, which is also used in a singleton pattern in design mode.

I hope this article will help you with your 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.