PHP spl_autoload_register function interface

Source: Internet
Author: User

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

 

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.