Spl_autoload_register
(PHP 5> = 5.1.2) spl_autoload_register-register _ autoload () function
Description
Bool spl_autoload_register ([callback $ autoload_function])
Register the function to the SPL _ autoload function stack. If the functions in the stack are not activated, activate them. If the _ autoload function has been implemented in your program, it must be explicitly registered to the _ autoload stack. Because the spl_autoload_register () function replaces the _ autoload function in Zend engine with spl_autoload () or spl_autoload_call ().
Note:SPL is the abbreviation of standard PHP Library (Standard PHP library. It is an extension library introduced by PhP5. Its main functions include the implementation of the autoload mechanism and various iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer autoload_func to the self-implemented function with the automatic loading function. SPL has two different functions: spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two function addresses.
Parameters
Autoload_function
The automatically loaded function to be registered. If no parameter is provided, the default implementation function spl_autoload () of autoload is automatically registered ().
Return Value
Return if successfulTrue, Or return when the failure occurs.False.
When PHP cannot find the class file, it will call this method. When it registers its own function or method, PHP will not call the _ autoload () function, but will call the custom function.
This function enables automatic loading. Many frameworks use this method to automatically load classes.
Example:
<? Php <br/> spl_autoload_register (function ($ class) {<br/> $ file = $ _ server ['document _ root']. '/predis/lib /'. strtr ($ class ,'//','/'). '. PHP '; <br/> If (file_exists ($ file) {<br/> require $ file; <br/> return true; <br/>}< br/>}); </P> <p> $ single_server = array (<br/> 'host' => '2017. 168.131.169 ', <br/> 'Port' => 6379, <br/> 'database' => 0 <br/> ); </P> <p> // automatically load the client class and the dependent class <br/> $ redis = new predis/clien T ($ single_server); </P> <p> $ category1 = json_encode (Array ("ID" => "1000211 ", "Category" => "domestic news"); <br/> $ category2 = json_encode (Array ("ID" => "1000212 ", "Category" => "International News"); <br/> $ category3 = json_encode (Array ("ID" => "1000213 ", "Category" => "Entertainment News"); <br/> $ category4 = json_encode (Array ("ID" => "1000214 ", "Category" => "Entertainment News"); <br/> $ category5 = json_encode (Array ("ID" => "1000215", "category" => "Entertainment News"); </P> <p> echo $ redis-> zadd ('news. CATEGORY ', 1000211, $ category1 ). '<br>'; <br/> echo $ redis-> zadd ('news. CATEGORY ', 1000215, $ category5 ). '<br>'; <br/> echo $ redis-> zadd ('news. CATEGORY ', 1000212, $ category2 ). '<br>'; <br/> echo $ redis-> zadd ('news. CATEGORY ', 1000214, $ category4 ). '<br>'; <br/> echo $ redis-> zadd ('news. CATEGORY ', 1000213, $ category3 ). '<br>'; </P> <p> // $ Category = $ redis-> zcount ('news. CATEGORY ', 100 0212,1000214); <br/> $ Category = $ redis-> zrangebyscore ('news. CATEGORY ', 1000212,1000214); <br/> foreach ($ category as $ value) <br/>{< br/> var_dump (json_decode ($ value )). '<br>'; <br/>}</P> <p> var_dump ($ redis-> zcard ('news. CATEGORY '); </P> <p> var_dump ($ redis-> zscore ('news. CATEGORY ', $ category3); </P> <p> var_dump ($ redis-> zrangebyscore ('news. CATEGORY ', 1000214,1000214); <br/>?>