Exploring SPL: Part I
“With the advent of PHP 5.0 came a new collection of built in classes and interfaces known as the Standard PHP Library (SPL). The goal of this library is to “solve standard problems and implements some efficient data access interfaces and classes.” What gives this library an edge over other third-party offerings is its tight integration into the core of the PHP language and data structures. In this series of blog posts, I will attempt to showcase some of the more useful components of this library.
I. An Introduction
Before we start rifling through SPL’s various interfaces and classes, let’s start things of with a basic overview. The SPL consists of a number of classes and interfaces that are designed to enhance PHP’s ability to work with and iterate over data.
Object Iteration
The library really shines in cases where iteration over an object’s data is necessary. Some basic interfaces such as the Iterator and the SeekableIterator provide the basic tools to do so. There are also some classes that implement these interfaces that can provide the needed functionality right out of the box, and also offer some excellent examples of how these interfaces can be used (take a look at the EmptyIterator and RegexIterator classes for instance).
Walks like an array, talks like an array, looks like an object….
Being tightly woven into the PHP core, SPL also provides the ability to provide access to data stored within an object as if the object were an array. How the data is stored within the object is entirely up to the developer, and could range from referencing a single internal array to the execution of complex code.
There is plenty more that SPL can do, and the release of PHP 5.3 and 6 both promis even more functionality. Now that I have you all worked up over SPL, you’ll have to wait for my next post to get into the nitty gritty details. In the meantime, consider browsing the official documentation located here.