Use the SPL spl_autoload_register and _ autoload methods in php

Source: Internet
Author: User
Tags spl

In php, The spl_autoload_register and _ autoload methods are only available in php5. Next I will introduce you to the usage of these two magic functions.

The spl_autoload_register () function should be one of the most used and very core functions in mainstream frameworks. It can automatically register functions and classes and implement functions similar to the _ autoload () function, it simplifies class calling and loading, and improves work efficiency.

Supported versions: PHP 5> = 5.1.2

Efficiency issues. The php manual says this:

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

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: Copy code

Class autoload
{
Public static function load ($ class name)
{
$ Filename = $ classname. ". class. php ";
If (file_exists ($ filename )){
Require_once $ filename;
}
}
}
 
Function _ autoload ($ class name)
{
// This is the default autoload method.
$ Filename = $ classname. ". class. php ";
If (file_exists ($ filename )){
Require_once $ filename;
}
}
 
// Register an autoloader
Spl_autoload_register ('autoload: load ');
Spl_autoload_register ('_ autoload ');
// Note: The following classes do not seem to be defined, but the system will automatically search for them based on the path provided by SQL _autoload_register.
// Foo. class. php file. If it is not found, an error is returned.
$ Foo = new foo ();
$ Foo-> bar ();
?>

In addition:

The _ autoload method will expire after spl_autoload_register, because the autoload_func function pointer has pointed to the spl_autoload Method
* You can add the _ autoload method to the autoload_functions list using the following method:

Spl_autoload_register ('_ autoload ');

In addition, we can use our custom loading method:

The first function:

The Code is as follows: Copy code

Function my_own_loader ($ classname)
{
$ Class_file = strtolower ($ classname). ". php ";
If (file_exists ($ class_file )){
Require_once ($ class_file );
}
}
 
Spl_autoload_register ("my_own_loader ");
 
$ A = new ();

Type 2:

The Code is as follows: Copy code

Class Loader
{
Public static function my_own_loader ($ classname)
{
$ Class_file = strtolower ($ classname). ". php ";
If (file_exists ($ class_file )){
Require_once ($ class_file );
}
}
}
 
// Pass the class and method names in the form of Arrays
Spl_autoload_register (array ("Loader", "my_own_loader "));
$ A = new ();

Instance: When the CI framework loads classes, the corresponding model is also generated.

The Code is as follows: Copy code

Static public function myAutoload ($ class ){
If (file_exists (APPPATH. 'models'. DIRECATORY_SEPARATOR. $ class. '. php ')){
Require_once APPPATH. 'models'. DIRECATORY_SEPARATOR. $ class. '. php ';
}
}
/**
* Register the loader
*/
Static public function autoload (){
Spl_autoload_register (array (_ CLASS __, 'myautoload '));
If (class_exists ('_ autoload ')){
Spl_autoload_register ('_ autoload ');
}
}
MY_Controller: autoload ();


Of course, the above is just the simplest example, __autoload just goes to include_path to find the class file and load it. we can define the _ autoload loading class rules as needed.

In addition, we can use spl_autoload_register to register our own autoload function if we do not want to call _ autoload when loading automatically, but call our own function (or class method. Its function prototype is as follows:
Bool spl_autoload_register ([callback $ autoload_function])

We will continue to rewrite the example above:

The Code is as follows: Copy code

View plaincopy to clipboardprint?
Function loader ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}

Spl_autoload_register ('loader ');

$ A = new ();

Function loader ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}

Spl_autoload_register ('loader ');

$ A = new ();

This way, php can run normally. At this time, when looking for classes, php does not call _ autoload, but calls our own function loader. In the same way, the following statements can also be used:

The Code is as follows: Copy code

View plaincopy to clipboardprint?
Class Loader
{
Public static function loadClass ($ class)
{
$ File = $ class. '. php ';
If (is_file ($ file )){
Require_once ($ file );
}
}
}

Spl_autoload_register (array ('loader ', 'loadclass '));

$ A = new ();


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.