PHP Implementation Lazy Loading method
This article mainly introduces the PHP implementation lazy loading method, the example analysis of PHP loading principle and lazy loading implementation skills, with a certain reference value, the need for friends can refer to the next
In this paper, we describe the lazy loading method for PHP. Share to everyone for your reference. The specific analysis is as follows:
Normal PHP loading is through the include (), require () and other methods to load the external file, and then through the instance call method or call static method directly, and this kind of write introduces the statement is very troublesome, some framework will be the specific path of the file all introduced, direct instantiation can be used, However, some class packages do not have to be used, the more the class package is written, the load of things is much, affecting the performance of the program.
A reflection class of the corresponding class can be obtained directly from the reflection class Reflectionclass of PHP:
The test.php file is as follows:
?
1 2 3 4 5 6 7 |
Class test{ Public Function ShowName () { Var_dump (__class__); } } ?> |
The index.php file is as follows:
?
1 2 3 4 5 6 7 8 9 Ten + + / / / + |
!--? php VA R_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 in the class name, you will get a reflection class of the corresponding class. Invoking newinstance with an instance will give an instance of the reflection class, thus completing the instantiation.
With the Get_included_files () method, we can see the file that is being introduced by the current page. Before instantiating the reflection class, only the index.php file, after instantiating the reflection class, automatically introduced a test.php file, then look at the above code, found that there is a __autoload () name of the Magic method, this method defines the automatic loading file, When Reflectionclass cannot find a class on the current page, __autoload () is called to load the class. This is the process of automatic loading.
To find out if the __autoload () method is open, you can view it by using PHP's standard library SPL method:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 |
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 (); } 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 method of automatic loading, there is currently a __autoload magic method, so the function name is returned, if the automatic loading method is not defined, the return is false, and Spl_autoload_ The register () method registers a method with the auto-load method by means of a method name, where the Newautoload method is used to replace the __autoload method.
In the Newautoload method, each successful execution, printing a word ' require success ', which is printed only once, shows that although the instance is 3 times reflectionclass (' test '), but because the test class has been loaded once, The automatic loading method is no longer performed. The getinstance () method of loading the class is much more convenient than the previous include (), just load the file that wrote the GetInstance () method.
The overridden auto-load method can define different file paths as needed by judging the name of the class. GetInstance can save an instance with static variables, which is also a singleton pattern used in design mode.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/963972.html www.bkjia.com true http://www.bkjia.com/PHPjc/963972.html techarticle PHP Implementation Lazy Loading method This article mainly introduces the PHP implementation lazy loading method, the example analysis of PHP loading principle and lazy loading implementation skills, with a certain reference value, need ...