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?
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
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?
<?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();
[php] view plaincopyprint?
if(false === spl_autoload_functions()){ if(function_exists('__autoload')){ spl_autoload_registe('__autoload',false); } }
spl_autoload_functions()函数会返回已注册函数的一个数组,如果SPL自动加载栈还没有被初始化,它会返回布尔值false。然后,检查是否有一个名为__autoload()的函数存在,如果存在,可以将它注册为自动加载栈中的第一个函数,从而保留它的功能。之后,可以继续注册自动加载函数。
还可以调用spl_autoload_register()函数以注册一个回调函数,而不是为函数提供一个字符串名称。如提供一个如array('class','method')这样的数组,使得可以使用某个对象的方法。
下一步,通过调用spl_autoload_call('className')函数,可以手动调用加载器,而不用尝试去使用那个类。这个函数可以和函数class_exists('className',false)组合在一起使用以尝试去加载一个类,并且在所有的自动加载器都不能找到那个类的情况下失败。
[php] view plaincopyprint?
f(spl_autoload_call('className') && class_exists('className',false)){ } else { }
以上就介绍了掼蛋游戏WEB版——PHP后台实现源码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。