_php tutorial for using SPL Spl_autoload_register and __autoload methods in PHP

Source: Internet
Author: User
Tags autoload spl
In PHP Spl_autoload_register and __autoload method is php5 only, the following I will give you the use of the two magic functions, you can enter to understand.

The Spl_autoload_register () function should be one of the most used and very central functions of the mainstream framework, enabling automatic registration of functions and classes, implementing functions like the __autoload () function, simplifying the invocation and loading of classes, and improving the efficiency of the work.

Supported version: PHP 5 >= 5.1.2

As for efficiency issues. The PHP manual has the following words:

BOOL Spl_autoload_register ([callback $autoload _function])

Register the function in the SPL __autoload function stack. If the functions in the stack have not been activated, they are activated. If you have implemented the __autoload function in your program, it must be explicitly registered in 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 in the SPL __autoload function stack. If the functions in the stack have not been activated, they are activated.
If you have implemented the __autoload function in your program, it must be explicitly registered in the __autoload stack. Because
The Spl_autoload_register () function replaces the __autoload function in Zend engine with the spl_autoload () or
Spl_autoload_call ().
Parameters
Autoload_function
The auto-load function to register. If no arguments are provided, the default implementation function for AutoLoad is automatically registered
Spl_autoload ().
return value
Returns TRUE if successful, and FALSE if it fails.
Note: SPL is the abbreviation for standard PHP library (PHP libraries). It is an extension library introduced by PHP5, whose main functions include the implementation of the AutoLoad mechanism and the inclusion of various iterator interfaces or classes. The implementation of the SPL autoload mechanism is achieved by pointing the function pointer autoload_func to its own implementation of the function that has the automatic loading function. SPL has two different function spl_autoload, Spl_autoload_call, to implement different automatic loading mechanisms by pointing autoload_func to these two different 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 a Autoloader
Spl_autoload_register (' autoload::load ');
Spl_autoload_register (' __autoload ');
Note: The following class does not appear to be defined, but the system is automatically searched based on the path provided by the Sql_autoload_register
foo.class.php file, if not found to error.
$foo = new Foo ();
$foo->bar ();
?>

Under the supplement:

The __autoload method is invalidated after spl_autoload_register because the Autoload_func function pointer is pointing to the Spl_autoload method
* The _autoload method can be added to the Autoload_functions list by the following method

Spl_autoload_register (' __autoload ');

In addition, we can also use our custom loading method:

The first type of 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 A ();

Type II:

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

Passing the names of classes and methods in the form of arrays
Spl_autoload_register (Array ("Loader", "My_own_loader"));
$a = new A ();

Example: When the CI framework implements the class loading, its corresponding model is also generated.

TD bgcolor= "#FFE7CE" height= "width=" 464 "> code 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 Loader
*/
static public function AutoLoad () {
Spl_autoload_register (array (__class__, ' Myautoload '));
if (class_exists (' __autoload ')) {
Spl_autoload_register (' __autoload ');
}
}
My_controller::autoload ();


Of course, above is just the simplest demonstration, __autoload just go to Include_path to find the class file and load, we can define the rules of __autoload load class according to our own needs.

In addition, if we do not want to invoke __autoload when loading automatically, but instead call our own function (or class method), we can use Spl_autoload_register to register our own autoload function. Its function prototype is as follows:
BOOL Spl_autoload_register ([callback $autoload _function])

We continue to rewrite the example above:

TD bgcolor= "#FFE7CE" height= "width=" 464 "> code as follows
copy code
View Plaincopy to Clipboardprint? !--? php

Function Loader ($class)
{
$file = $class. '. php ';
if (Is_file ($file)) {
require_once ($file);
}
}

Spl_autoload_register (' loader ');

$a = new A ();

!--? php
function Loader ($class)
{
$file = $class. '. php ';
if (Is_file ($file)) {
require_once ($file);
}
}

Spl_autoload_register (' loader ');

$a = new A ();

This can also work properly, when PHP is looking for classes without calling __autoload instead of calling our own defined function loader. In the same way, the following is also possible:

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


http://www.bkjia.com/PHPjc/445632.html www.bkjia.com true http://www.bkjia.com/PHPjc/445632.html techarticle in PHP spl_autoload_register and __autoload method is php5 only, the following I will give you the use of the two magic functions, you can enter to understand. Spl_autoload_reg ...

  • 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.