PHP Implementation Lazy Loading method, PHP implementation load _php Tutorial

Source: Internet
Author: User
Tags php programming

PHP Implementation Lazy Loading method, PHP implementation load


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:

<?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 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:

Var_dump (Spl_autoload_functions ()); Spl_autoload_register (' newautoload '); Var_dump (Spl_autoload_functions ()); $ TestObj1 = 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 SUCCE SS ' (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/963826.html www.bkjia.com true http://www.bkjia.com/PHPjc/963826.html techarticle PHP Implementation Lazy loading method, PHP Implementation load This article describes the PHP implementation lazy loading method. Share to everyone for your reference. The specific analysis is as follows: normal PHP loading is 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.