The fallen gem of PHPSPL. RafaelDohms I was amazed by the previous article. I couldn't help but translate it and added some content. SPL, PHP Standard Library (StandardPHPLibrary), this article from Rafael Dohms in PHP5.0 impressed me, I couldn't help but translate it, and added some content.
SPL and PHP Standard Library (Standard PHP Library), which are built-in components and interfaces from PHP 5.0 and 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, and spl.AutoloadRegister, splClasses, iteratorApply and other help functions are used to 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.
As you can see, splAutoloadRegister can also add multiple loading logics in the form of arrays. You can also use the splAutoloadUnregister removes 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:
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 :)
The following are other iterators provided by SPL:
- RecursiveIterator
- RecursiveIteratorIterator
- OuterIterator
- IteratorIterator
- FilterIterator
- RecursiveFilterIterator
- ParentIterator
- SeekableIterator
- LimitIterator
- GlobIterator
- CachingIterator
- RecursiveCachingIterator
- NoRewindIterator
- AppendIterator
- RecursiveIteratorIterator
- InfiniteIterator
- RegexIterator
- RecursiveRegexIterator
- EmptyIterator
- RecursiveTreeIterator
- ArrayIterator
Since PHP5.3, there will be more built-in iterators. I think you can try it, maybe it will change your habit of writing traditional code.
SplFixedArraySPL also has a series of built-in array operations tools. for example, you can use SplFixedArray to instantiate an array with a fixed length. So why use it? Because it is faster, even it is related to your salary problem :)
We know that regular PHP arrays contain different types of keys, such as numbers and strings, and the length is variable. It is precisely because of these "advanced features" that PHP uses hash to obtain the corresponding value through the key-in fact, this may cause performance problems in specific situations.
Because SplFixedArray uses a fixed number key, it does not use hash storage. Not exactly. you can even think of it as a C array. This is why the SplFixedArray is faster than the usual array (only in PHP5.3 ).
So how fast is it? the following group of data can give you a glimpse of it.
If you need a lot of array operations, you can try and believe it is trustworthy.
Data structureAt the same time, SPL also provides implementation of some basic data structures. Although we can use traditional variable types to describe the data structure, for example, using arrays to describe the stack (Strack) -- then using the corresponding pop and push (arrayPop (), arrayPush (), but you must always be careful, because after all they are not specifically used to describe the data structure-a misoperation may damage the stack.
The SPL SplStack object strictly describes data in the form of stacks and provides corresponding methods. At the same time, such code should be able to understand its operation stack rather than an array, so that your peers can better understand the corresponding code and it is faster.
Finally, the above pale examples may not be enough to "seduce you" to use SPL. To learn more about SPL, you need to mine more and more powerful SPL functions. But it is carved as a gem to shine.
I was amazed by the article above http://www.bkjia.com/PHPjc/777572.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/777572.htmlTechArticleRafael Dohms. I couldn't help but translate it and added some content. SPL, PHP Standard Library (Standard PHP Library), starting from PHP 5.0...