7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 What Is the SPL?

So what is the SPL? In this lesson, I’ll explain how the SPL allows you to write classes which hook directly in to the power of PHP, whether you’re looping over data, handling exceptions or creating data structures. In short, the SPL allows us to overload, or augment, some of PHP’s basic lexicographical constructs.

Related Links

2.3 What Is the SPL?

So what is the SPL? If this is your first time hearing about it, it was originally authored by Marcus Berger and released with PHP version 5.0. I think the PHP manual does a great job of succinctly explaining it. The standard PHP library, or SPL, is a collection of interfaces and classes that are meant to solve common problems. The SPL provides a set of standard data structures, a set of iterators to diverse other objects, a set of interfaces, a set of standard exceptions, a number of classes to work with files, and it provides a set of functions like spl_autoload_register. Said another way, it allows you to write classes which hook directly into the power of PHP, whether you're looping over data or creating data structures, working with exceptions and so on. In short, it allows us to overload or augment some of PHP's basic lexical graphical constructs. Now that we've looked at the SPL quite broadly, what exactly are iterators? I think Wikipedia gives the simplest definition of an iterator, when it says that an iterator is an object that enables a programmer to traverse a container, particularly lists. And in my opinion, iterators are simply a magical addition to PHP. Magical perhaps is a little superfluous, but I'm just trying to sum up the feeling of the simplicity and the efficiency that you get when you start using them, and start using them properly. Anyway, cutting from feelings to specifics, looking at the definition of the iterator class, you can see that it's quite a small class with only five methods, those being current, key, next, rewind, and valid. These provide for the basic functionality required iterator data list. Stepping through them, current retrieves the current item being looked at in the list. Key retrieves the current item's key or indexed in the list. Next moves the list pointer to the next item, assuming it's available. Rewind, conversely, moves the list pointer to the previous item in the list, once again, if it's available. And valid, which we'll see much more of later cuz it's really, really important, determines if the item is able to be used, and also, whilst we're looking at the class and note that it implements traversable. Traversable is an interface which implements no methods. Now, this might seem like a little bit of a strange thing to do, but the purpose of it is to identify whether a class can be traversed using PHP's for each. Now let's have a look at the specific iterators, which we'll be using during the course, of which there are four. Starting with array iterator, this iterator allows us to iterate over arrays and objects, uncertain in modifying values and keys. This might not seem significant, but there's power in that simplicity. Looking at the list of methods, you can see that it builds on iterator's functionality, allowing for adding new items, sorting items in a variety of ways, moving forward and backward through the list, and retrieving items from the list. Note also that it implements four interfaces. These are array access, which provides methods for addressing an object like an array. Seekable iterator, which provides the seek method for moving to a specific position in a dataset. Countable, which provides the count method so PHP's count function can be used to get the object's size. And finally, serializable. This provides the serialized and unserialized methods, this serializing and unserializing the dataset when PHPs sleep and wake up magic methods are invoked. Next, there's the DirectoryIterator class. As we can see here, it provides a simple interface plus methods for viewing the contents of file system directories. You can see that it allows for retrieving a file or a directory or a sim link and then looking at its size, type, permissions, and a variety of other properties. By extending the SplFileInfo class, it allows us to iterate other directories quite powerfully, working with files as though they're objects. And in the process, filtering out unwanted files based on type, size, and extension, as well as a number of other criteria. Thirdly, we'll be using filter iterator. This abstract iterator filters out unwanted values from an existing iterator. It implements iterator, iterator sorry that's a bit of a tongue twister, which allows the conversion of anything that is traversable into an iterator. Why is this a good thing, you might ask? Say that we only wanted to view episodes which have a publish date after today. We can, as we'll see later, add logic to determine if they're published after the current date or not, and include the ones which aren't. Finally, we'll use the LimitIterator class. The LimitIterator allows for iteration over a limited subset of items in an iterator. Say, for example, we wanted to implement basic pagination, or on the home page, we only wanted to display the top three or five records. Using the LimitIterator, we can limit the number of records retrieved from the data set of an existing iterator. And that's it for this video. In the next video, we'll start looking at the core classes, which the classes we'll write will interact within use.

Back to the top