: This article mainly introduces the WEB version of egg game-PHP background implementation source code. if you are interested in PHP tutorials, please refer to it. The following are some background source code for the WEB version of egg games, all of which are 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 in the _ autoload () function, the class name corresponds to the actual disk file, you can achieve the lazy loading effect. Here we can also see that the most important thing in the implementation of the _ autoload () function is the implementation of the ing rule between the class name and the actual disk file.
But now the problem arises. if many other class libraries need to be used in the implementation of a system, these class libraries may be developed by different development engineers, the ing rules for class names and actual disk files are different. In this case, to automatically load the class library file, all the ing rules must be implemented in the _ autoload () function. Therefore, the _ autoload () function may be very complex, it cannot even be implemented. In the end, the _ autoload () function may be very bloated. even if it can be implemented, it will bring a huge negative impact to future maintenance and system efficiency. In this case, introduce the SPL standard library in PHP5, a new solution, namely the spl_autoload_register () function.
2. spl_autoload_register () function
This function registers the function to the _ autoload function stack of SPL and removes the default _ autoload () function. The following example shows that:
[Cpp] view plaincopyprint?
- Function _ autoload ($ class_name ){
- Echo '_ autload class:', $ class_name ,'
';
- }
- Function classLoader ($ class_name ){
- Echo 'SPL load class: ', $ class_name ,'
';
- }
- Spl_autoload_register ('classloader ');
- New Test (); // Result: SPL load class: Test
Function _ autoload ($ class_name) {echo '_ autload class:', $ class_name ,'
';} Function classLoader ($ class_name) {echo 'SPL load class:', $ class_name ,'
';} Spl_autoload_register ('classloader'); new Test (); // Result: SPL load class: Test
Syntax: bool spl_autoload_register ([callback $ autoload_function]) accepts two parameters: one is the function added to the auto-load stack, and the other is the flag that the loader cannot find whether to throw an exception when the class is found. The first parameter is optional and is directed to the spl_autoload () function by default. This function will automatically find the class name with lower case in the path. php extension or. ini extension, or any file registered with another extension in the spl_autoload_extensions () function.
[Php] view plaincopyprint?
-
- Class CalssLoader
- {
- Public static function loader ($ classname)
- {
- $ Class_file = strtolower ($ classname). ". php ";
- If (file_exists ($ class_file )){
- Require_once ($ class_file );
- }
- }
- }
- // The method is static.
- Spl_autoload_register ('calssloader: loader ');
- $ Test = new Test ();
Once the spl_autoload_register () function is called, the system calls all functions registered to the spl_autoload_register () function in sequence, instead of automatically calling the _ autoload () function. To avoid this situation, you need to use a safer method for calling the spl_autoload_register () function:
[Php] view plaincopyprint?
- If (false === spl_autoload_functions ()){
- If (function_exists ('_ autoload ')){
- Spl_autoload_registe ('_ autoload', false );
- }
- }
if(false === spl_autoload_functions()){ if(function_exists('__autoload')){ spl_autoload_registe('__autoload',false); } } The spl_autoload_functions () function returns an array of the registered functions. if the SPL automatically loads the stack and is not initialized yet, it returns a boolean value of false. Then, check whether a function named _ autoload () exists. If yes, you can register it as the first function in the auto load stack to retain its function. Then, you can continue to register the auto-load function.
You can also call the spl_autoload_register () function to register a callback function, instead of providing a String name for the function. If you provide an array such as array ('class', 'method'), you can use the method of an object.
Next, by calling the spl_autoload_call ('classname') function, you can manually call the loader without trying to use that class. This function can be combined with the class_exists ('classname', false) function to try to load a class, in addition, if no auto-loader can find the class, it fails.
[Php] view plaincopyprint?
- F (spl_autoload_call ('classname') & class_exists ('classname', false )){
-
- } Else {
- }
f(spl_autoload_call('className') && class_exists('className',false)){ } else { } The SPL automatic loading function is provided by the spl_autoload (), spl_autoload_register (), spl_autoload_functions (), spl_autoload_extensions (), and spl_autoload_call () functions.Zookeeper
The above introduces the egg game WEB version-PHP background implementation source code, including the content, hope to be helpful to friends interested in PHP tutorials.