spl,php Standard library (Standard PHP library), which is a built-in component and interface from PHP 5.0, and is gradually maturing from PHP5.3. SPL is actually built into all PHP5 development environments without any setup.
It seems like a lot of PHP developers are basically not using it, even unheard of. The reason for this can be traced to its highbrow document, so that you ignore the "it exists." This gem, like Titanic's "Heart of the Sea", was sunk to the bottom of the sea. And now it should be picked up by us and put it in its rightful place, which is the point of this article.
So, what does SPL offer?
SPL extends the PHP engine, such as the arrayaccess, countable, and Seekableiterator interfaces, which are used to manipulate objects as arrays. At the same time, you can also use Recursiveiterator, arrayobejcts and other iterators for data iterative operations.
It also has built-in help functions for several objects such as exceptions, Splobserver, Spltorage, and Splautoloadregister, Splclasses, iteratorapply, and so on (helper functions), for overloading the corresponding functionality.
These tools come together as a versatile Swiss Army knife, making it possible to improve PHP's code efficiency in a qualitative and efficient form. So, how do we play its power?
Overload Autoloader
If you're a "textbook programmer", then you're guaranteed to know how to use __autoload instead of includes/requires to load the corresponding class, right?
But long, you will find that you have been in trouble, first of all, you have to ensure that your class files must be in the specified file path, such as in the Zend framework you must use "_" to split the class, method name (How do you solve this problem?) )。
Another problem is that when the project becomes more and more complex, the logic within the __autoload becomes correspondingly complex. In the end, you'll even add the anomaly judgment and write all the logic of the loaded class into it.
We all know that "eggs can't be put in a basket", using SPL to separate __autoload loading logic. Just write your own autoload function and then overload it with the function provided by SPL.
For example, the above Zend framework problem, you can overload Zend Loader corresponding method, if it does not find the corresponding class, then use the function you defined earlier.
Copy Code code as follows:
<?php
Class Myloader {
public static function Doautoload ($class) {
This module corresponds to the autoload operation
}
}
Spl_autoload_register (Array (' Myloader ', ' doautoload '));
?>
As you can see, SPL autoload Register also adds multiple load logic as an array. At the same time, you can also use the SPL autoload unregister to remove the load logic that is no longer needed, and this functionality is always used.
Iterators
Iterations are one of the common design patterns that are commonly applied to a uniform traversal operation in a set of data. It is no exaggeration to say that SPL provides all the types of iterators you need for the corresponding data type.
A very good case is to traverse the directory. The general practice is to use Scandir, and then skip "." and "..", and other files that do not meet the criteria. For example, you need to traverse a directory to extract the image files, you need to determine whether a JPG, GIF end.
The following code is an example of using SPL's iterator to perform the recursive search for a picture file in a specified directory:
Copy Code code as follows:
<?php
Class Recursivefilefilteriterator extends Filteriterator {
Extension that satisfies 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 to see if the file name extension satisfies the criteria
*/
Public Function accept () {
$item = $this->getinneriterator ();
if ($item->isfile () &&
In_array (PathInfo ($item->getfilename (), pathinfo_extension), $this->ext)) {
return TRUE;
}
}
}
Instantiation of
foreach (New Recursivefilefilteriterator ('/path/to/something ') as $item) {
Echo $item. Php_eol;
}
?>
You might say, isn't it going to take more code to do the same thing? So, looking at the code above, don't you have a highly reusable and testable code:)