Php spl_autoload_register and _ autoload

Source: Internet
Author: User
Tags autoload php tutorial

Php Tutorial spl_autoload_register and _ autoload

When talking about the automatic loading class of the framework, I roughly flipped through it. Currently, mainstream framework systems use the spl_autoload_register function instead of The _ autoload function.

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 ();
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:

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: 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 ("my_own_loader", "Loader "));

$ A = new ();
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. It seems that he points to the same stack, and the efficiency is always the problem of brother two.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.