PHP automatic object loading function spl_autoload_register

Source: Internet
Author: User
Spl_autoload_register (PHP5 & gt; 5.1.2) spl_autoload_register & mdash; register _ autoload () function description boolspl_autoload_register ([callback $ autoload_function])

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 ().

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
If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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.

Example
Load. php file

Class LOAD {static function loadClass ($ class_name) {$ filename = $ class_name. ". class. php "; $ path =" include /". $ filename; if (is_file ($ path) return include $ path ;}}/*** sets the automatic loading of an object * spl_autoload_register-Register given function as _ autoload () implementation */spl_autoload_register (array ('load', 'loadclass');/*** _ autoload method will expire after spl_autoload_register, because the autoload_func function pointer has pointed to the spl_autoload method *, you can use the following method to add the _ autoload method to the autoload_functions list */spl_autoload_register ('_ autoload ');

If you use spl_autoload_register to register a class method and the _ autoload function,
According to the registration sequence, if the class file is loaded in the first registered method or function, the method or function of the second registered class will no longer be executed. Otherwise, the method or function of the second registered class will be executed.

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.