SPL in PHP

Source: Internet
Author: User
Tags spl php framework php script

Spl_autoload is the default automatic load function that SPL implements, its function is relatively simple. It can receive two parameters, the first parameter is $class_name, the class name, the second parameter $file_extensions is optional, the extension of the class file "title=" extension > extension, can be in the $file_ extensions specifies multiple extension "title=" Extensions > extensions, separated by semicolons; if not specified, it will use the default extension "title=" extension > extension. inc or. php. Spl_autoload first converts $class_name to lowercase, and then searches for $class_name.inc or $class_name.php files in all include path (if you do not specify $file_extensions parameters , and if found, loads the class file. You can manually use Spl_autoload ("person", ". class.php") to load the person class. In fact, it's about the same as require/include, and it can specify more than one extension "title=" extension > extension.

How to let Spl_autoload function automatically, that is, will autoload_func point to Spl_autoload? The answer is to use the Spl_autoload_register function. The first time you call Spl_autoload_register () in a PHP script without using any arguments, you can point the Autoload_func to Spl_autoload.

We know from the above instructions that the Spl_autoload function is relatively simple and that it is implemented in SPL extensions and we cannot extend its functionality. What if you want to implement your own more flexible automatic loading mechanism? At this time, the Spl_autoload_call function shines debut.

Let's take a look at the wonders of Spl_autoload_call's implementation. Inside the SPL module, there is a global variable autoload_functions, which is essentially a hashtable, but we can simply look at it as a list, and each element in the list is a function pointer to a function that has the function of loading the class automatically. The implementation of the Spl_autoload_call itself is simple, just a simple sequential execution of each function in the list, after each function is executed to determine whether the required class has been loaded, if the load succeeds directly back, no longer continue to execute the list of other functions. If all the functions in this list are completed and the class is not loaded, the spl_autoload_call exits without reporting an error to the user. Therefore, the use of the autoload mechanism, and can not guarantee that the class will be able to correctly automatically load, the key is to see how your automatic load function to achieve.

The standard library method in PHP5 Spl_autoload is equivalent to implementing its 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 automatically looks for a. php/.inc file with the same name as $classname under the registry directory. Of course, you can also specify a particular type of file by registering the extension

The

code is as follows:


<?php


spl_autoload_extensions ('. Php,.inc,.some ');

In this way, it also searches for the. some file. By default, PHP will not start spl_autoload, so how to automatically let Spl_autoload effective? Method is

The

code is as follows:


<?php


Spl_autoload_register ();

Spl_autoload_register has a $callback parameter, if not specified, it will automatically register spl_autoload, in order to be able to search for more automatic loading directory, you can set up the automatic loading directory before these code

The

code is as follows:


<?php


Set_include_path (Get_include_path (). Path_separator. ' Some/path '. Directory_separator);

In this way, when PHP cannot find the specified class, it will look for it in the directory specified by Set_include_path.

These methods are commonly used in the PHP framework. For example, link up the above description:

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 load the ClassA class under Some/path, it looks for classa.php or Classa.inc or classa.some in the directory so you can safely use the new ClassA or extends ClassA

The

code is as follows:


<?php


ClassB extends ClassA {


//code.


    }

$a = new ClassA;
$b = new ClassB;

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.