WEB version of Egg Breaking Game - PHP backend implementation source code

WBOY
Release: 2016-08-08 09:31:43
Original
2161 people have browsed it

The following is part of the background source code of the WEB version of the game, and all source codes will be released one after another. When there are a large number of class files to be included, we only need to determine the corresponding rules, and then match the class names with the actual disk files in the __autoload() function to achieve the lazy loading effect. From here we can also see that the most important thing in the implementation of the __autoload() function is the implementation of the mapping rules between the class name and the actual disk file.​

But now comes the problem. If in the implementation of a system, if many other class libraries need to be used, these class libraries may be developed by different development engineers, and the mapping rules between their class names and actual disk files are different. . At this time, if you want to implement automatic loading of class library files, you must implement all mapping rules in the __autoload() function. Therefore, the __autoload() function may be very complicated or even impossible to implement. In the end, the __autoload() function may become very bloated. Even if it can be implemented, it will have a great negative impact on future maintenance and system efficiency. In this case, The SPL standard library is introduced in PHP5, A new solution is the spl_autoload_register() function.

2, spl_autoload_register() function

The function of this function is to register the function into the __autoload function stack of SPL and remove the system default _ _autoload() function. This can be seen in the following example:

[cpp] view plaincopyprint?

  1. function __autoload($class_name) {
  2. echo '__autload class:', $class_name, '
    '
    ;
  3. }
  4. function classLoader($class_name) {
  5. echo 'SPL load class:', $class_name, '
    '
    ;
  6. }
  7. spl_autoload_register('classLoader');
  8. new Test();//Result: SPL load class:Test
function __autoload($class_name) {
    echo '__autload class:', $class_name, '<br />';
}
function classLoader($class_name) {
    echo 'SPL load class:', $class_name, '<br />';
}
spl_autoload_register('classLoader');
new Test();//结果:SPL load class:Test
Copy after login

Syntax: bool spl_autoload_register ( [callback $autoload_function] ) accepts two parameters: one is the function added to the autoloading stack, and the other is a flag indicating whether to throw an exception when the loader cannot find this class. The first argument is optional and by default points to the spl_autoload() function, which automatically looks for any extension in the path that has a lowercase class name and a .php extension or .ini extension, or any other extension registered with the spl_autoload_extensions() function. named file.

[php] view plaincopyprint?

  1. public
  2. static
  3. function
  4. loader(
  5. $classname
  6. )                                                                                                    $classname).".php"; if (file_exists($class_file
  7. )){
  8. $class_file); }                                                                    $test
  9. =
  10. new Test();
    <?php  
    class CalssLoader   
    {   
        public static function loader($classname)   
        {   
            $class_file = strtolower($classname).".php";   
            if (file_exists($class_file)){   
                require_once($class_file);   
            }   
        }   
    }    
    // 方法为静态方法   
    spl_autoload_register('CalssLoader::loader');    
    $test = new Test();
    Copy after login
          一旦调用spl_autoload_register()函数,当调用未定义类时,系统会按顺序调用注册到spl_autoload_register()函数的所有函数,而不是自动调用__autoload()函数。如果要避免这种情况,需采用一种更加安全的spl_autoload_register()函数的初始化调用方法:

    [php] view plaincopyprint?

    1. if(false === spl_autoload_functions()){      
    2.     if(function_exists('__autoload')){      
    3.         spl_autoload_registe('__autoload',false);      
    4.     }      
    5.  }     
    if(false === spl_autoload_functions()){    
        if(function_exists('__autoload')){    
            spl_autoload_registe('__autoload',false);    
        }    
     }   
    Copy after login

    spl_autoload_functions()函数会返回已注册函数的一个数组,如果SPL自动加载栈还没有被初始化,它会返回布尔值false。然后,检查是否有一个名为__autoload()的函数存在,如果存在,可以将它注册为自动加载栈中的第一个函数,从而保留它的功能。之后,可以继续注册自动加载函数。

    还可以调用spl_autoload_register()函数以注册一个回调函数,而不是为函数提供一个字符串名称。如提供一个如array('class','method')这样的数组,使得可以使用某个对象的方法。

    下一步,通过调用spl_autoload_call('className')函数,可以手动调用加载器,而不用尝试去使用那个类。这个函数可以和函数class_exists('className',false)组合在一起使用以尝试去加载一个类,并且在所有的自动加载器都不能找到那个类的情况下失败。

    [php] view plaincopyprint?

    1. f(spl_autoload_call('className') && class_exists('className',false)){      
    2.     
    3.     } else {      
    4.     }     
    f(spl_autoload_call('className') && class_exists('className',false)){    
      
        } else {    
        }   
    Copy after login
    SPL自动加载功能是由spl_autoload() ,spl_autoload_register(), spl_autoload_functions() ,spl_autoload_extensions()和spl_autoload_call()函数提供的。

    

    以上就介绍了掼蛋游戏WEB版——PHP后台实现源码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template