SPL spl_autoload_register and _ autoload examples

Source: Internet
Author: User
Tags autoload spl

Introduction: This is a detailed page of SPL spl_autoload_register and _ autoload usage examples. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339372 'rolling = 'no'>

I have been writing a framework recently. Of course, I have learned a lot from my previous writing frameworks. Haha.

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.

Amount... For example, do not use _ autoload ????

Powerful functions... Hefei network product Center

Example:

 

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

 

 

Note the following:

Hefei Net Product Center Co., http://lab.wehefei.com.

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

 

Hefei Net Product Center Co., http://lab.wehefei.com.

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:

BoolSpl_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 the problem of Brother Two, haha ..

If you are interested, you can test it ..

Article Source: Hefei Net Product Center Co., http://lab.wehefei.com/posts/286.

Http://lab.wehefei.com/posts/311

 

 

Love J2EE follow Java Michael Jackson video station JSON online tools

http://biancheng.dnbcw.info/php/339372.html pageno: 8

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.