PHPSPL usage and its power

Source: Internet
Author: User
Tags autoload spl
What is SPL and how to use it? what is its function? let's talk about the usage of PHPSPL and the Standard PHP Library ), the built-in components and interfaces from PHP 5.0 have gradually matured from PHP5.3. In fact, SPL is built in all PHP5 development environments without any settings.
It seems that many PHP developers have hardly used it, or even heard of it. The reason is that you can trace its descriptive documents, so that you ignore "its existence 」. SPL is like the heart of the ocean of Titani and is sunk to the bottom of the sea. Now it should be picked up by US and dressed in its proper position. this is also the point of view to be expressed in this article.
So what does SPL provide?
SPL extends the PHP engine, such as ArrayAccess, Countable, and SeekableIterator interfaces. they are used to operate objects in arrays. You can also use RecursiveIterator, ArrayObejcts, and other iterators to perform data iteration operations.
It also has several built-in objects such as Exceptions, SplObserver, Spltorage, splautoloadregister, splclasses, iteratorapply, and other help functions (helper functions) for reload the corresponding functions.
The aggregation of these tools is like using the versatile Swiss Army knife to improve PHP code efficiency. So how can we exert its power?
Reload autoloader
If you are a textbook programmer, you are sure to understand how to use _ autoload to replace the events des/requires operation to load the corresponding classes in inertia, right?
But for a long time, you will find that you are in trouble. First, you must ensure that your class files must be in the specified file path, for example, in the Zend Framework, you must use "_" to separate classes and method names (how do you solve this problem ?).
Another problem is that when the project becomes more and more complex, the logic in _ autoload will also become more complex. At the end, you may even add exception judgment and write the logic of all loading classes, such as the number.
We all know that "eggs cannot be placed in one basket", and SPL can be used to separate the loading logic of _ autoload. You only need to write your own autoload function, and then use the function provided by SPL to reload it.
For example, in the above Zend Framework, you can reload the method corresponding to Zend loader. if it does not find the corresponding class, use the function you previously defined.
The code is as follows:
Class MyLoader {
Public static function doAutoload ($ class ){
// The autoload operation corresponding to this module
}
}

Spl_autoload_register (array ('myloader ', 'doautoload '));
?>

As you can see, spl autoload register can also add multiple loading logics in the form of arrays. At the same time, you can also use spl autoload unregister to remove the unnecessary loading logic, which is always used.
Iterator
Iteration is one of the common design patterns and is widely used in a set of data for unified traversal operations. It is no exaggeration to say that SPL provides all the corresponding data type iterators you need.
A good case is to traverse the directory. The common practice is to use scandir and skip "." and "..." and other files that do not meet the conditions. For example, if you need to traverse a directory to extract the image files, you need to determine whether it is jpg or gif.
The following code uses the SPL iterator to execute the above recursive search for image files in a specified directory:
The code is as follows:
Class RecursiveFileFilterIterator extends FilterIterator {
// The extension that meets the condition
Protected $ ext = array ('jpg ', 'GIF ');
/**
* Provide $ path and generate the corresponding directory iterator
*/
Public function _ construct ($ path ){
Parent: :__ construct (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ path )));
}
/**
* Check whether the file extension meets the conditions
*/
Public function accept (){
$ Item = $ this-> getInnerIterator ();
If ($ item-> isFile ()&&
In_array (pathinfo ($ item-> getFilename (), PATHINFO_EXTENSION), $ this-> ext )){
Return TRUE;
}
}
}
// Instantiate
Foreach (new RecursiveFileFilterIterator ('/path/to/something') as $ item ){
Echo $ item. PHP_EOL;
}
?>

You may say, isn't it because you have spent more code to do the same thing? Then, check the code above. didn't you have code that is highly reusable and can be tested :)

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.