Php spl fallen gem

Source: Internet
Author: User
Tags autoload spl

Rafael Dohms I was amazed by the previous article. 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.


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.


    SPL 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 Structure

    At 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.

Related Article

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: 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.