PHP function spl_autoload_register () usage and _ autoload () introduction _ PHP Tutorial

Source: Internet
Author: User
PHP function spl_autoload_register () usage and _ autoload () Introduction. _ Autoload () is not used anymore. it has been mentioned in my WEB development notes before. PHP _ autoload function (automatic loading of class files), original address: www. the usage of jb _ autoload () will not be repeated. it has been mentioned in my WEB development notes before. PHP _ autoload function (automatically load class files) use method, original address: http://www.jb51.net/article/29625.htm.
Let's talk about the usage of spl_autoload_register (). it's easy to understand that it is to declare A custom _ autoload (). you can be A function or B function, the method of writing the function body should be the same as _ autoload.
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.
Spl_autoload_register ('func _ name ');
Spl_autoload_register (array ('class _ name', 'method _ name '));
Details are as follows:
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 function of autoload is automatically registered.
Spl_autoload ().
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.
The code is as follows:
Test. class. php

The code is as follows:


Class abc {
Function _ construct ()
{
Echo 'www .chhua.com;
}
}
?>


Load. php

The code is as follows:


Class LOAD
{
Static function loadClass ($ class_name)
{
$ Filename = $ class_name. ". class. php ";
If (is_file ($ filename) return include_once $ filename;
}
}
/**
* Set automatic object loading
* Spl_autoload_register-Register given function as _ autoload () implementation
*/
Spl_autoload_register (array ('load', 'loadclass '));
$ A = new Test (); // automatically loads classes. many frameworks use this method to automatically load classes.
?>

The use of http://www.bkjia.com/PHPjc/325046.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325046.htmlTechArticle__autoload () will not be said again, I have already said in my WEB development notes. PHP _ autoload function (automatic loading class files) use method, original address: http://www.jb...

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.