Spl_autoload in php

Source: Internet
Author: User
Tags autoload spl php framework
SPL is the abbreviation of StandardPHPLibrary (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 SPLautoload mechanism is implemented by pointing the function pointer autoload_func to the self-SPL. There are two different functions spl_autoload and spl_autoload_call. different automatic loading mechanisms are implemented by pointing autoload_func to these two function addresses.

Spl_autoload is the default automatic loading function implemented by SPL, and its functions are relatively simple. It can receive two parameters. The first parameter is $ class_name, indicating the class name. The second parameter $ file_extensions is optional, indicates the extension of the class file. you can specify multiple extensions in $ file_extensions, the protection names are separated by semicolons. if not specified, it uses the default extension "title =" extension "> extension. inc or. php. Spl_autoload first converts $ class_name to lowercase, and then searches for the $ class_name.inc or $ class_name.php file in all include paths (if the $ file_extensions parameter is not specified). If yes, load the file. You can manually use spl_autoload ("Person", ". class. php") to load the Person class. Actually, it is similar to require/include. different from require/include, it can specify multiple "title =" extension "> Extensions.

How can we make spl_autoload automatically take effect, that is, direct autoload_func to spl_autoload? The answer is to use the spl_autoload_register function. If you do not use any parameters when calling spl_autoload_register () for the first time in a PHP script, you can direct autoload_func to spl_autoload.

Through the above description, we know that the spl_autoload function is relatively simple, and it is implemented in the SPL extension, and we cannot expand its function. What if I want to implement a more flexible automatic loading mechanism? The spl_autoload_call function is now available.

Let's take a look at what's wonderful about the implementation of spl_autoload_call. In the SPL module, there is a global variable autoload_functions, which is essentially a HashTable, but we can simply think of it as a linked list. every element in the linked list is a function pointer, point to a function with the automatic loading class function. The implementation of spl_autoload_call is very simple, but it simply executes each function in the linked list in order, and judges whether the required class has been loaded once after each function is executed, if the load is successful, the system returns directly and does not continue to execute other functions in the linked list. If all the functions in this linked list have not been loaded after execution, spl_autoload_call will exit without reporting an error to the user. Therefore, using the autoload mechanism does not guarantee that the class can be automatically loaded correctly. The key is how to implement the automatic loading function.

The standard library method spl_autoload in php5 is equivalent to implementing your own _ autoload

The code is as follows:


<? Php
Function _ autoload ($ classname ){
If (is_file ($ classname. '. php '){
Include $ classname. '. php ';
} Elseif (is_file ($ classname. '. Inc '){
Include $ classname. '. Inc ';
}
}

It will automatically search for the. php/. inc file with the same name as $ classname under the Registration Directory. Of course, you can also specify a specific type of file by registering the extension.

The code is as follows:


<? Php
Spl_autoload_extensions ('. php,. inc,. some ');

In this way, it will also search for. some files. By default, php does not start spl_autoload. how can we automatically make spl_autoload take effect? The method is

The code is as follows:


<? Php
Spl_autoload_register ();

Spl_autoload_register has a $ callback parameter. If this parameter is not specified, it automatically registers spl_autoload. to search for more automatically loaded directories, you can set automatic directory loading before the code.

The code is as follows:


<? Php
Set_include_path (get_include_path (). PATH_SEPARATOR. 'some/path'. DIRECTORY_SEPARATOR );

In this way, if php cannot find the specified class, it will find it in the directory specified by set_include_path.

These methods are often used in the php framework. For example, concatenate the above introduction:

The code is as follows:


<? Php
Set_include_path (get_include_path (). PATH_SEPARATOR. 'some/path'. DIRECTORY_SEPARATOR );
Spl_autoload_extensions ('. php,. inc,. some ');
Spl_autoload_register ();

When you want to load the classA class under some/path, it will search for classa in the directory. php or classa. inc or classa. some, so that you can safely use new classA or extends classA

The code is as follows:


<? Php
ClassB extends ClassA {
// Code ..
}

$ A = new ClassA;
$ B = new ClassB;

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.