Join Justin Yost for an in-depth discussion in this video Create an iterator, part of PHP: Advanced Topics.
- [Instructor] Let's build an iterator now. There is a code sample, snippets.txt, provided as part of the exercise files for this lesson. If we open it up, we'll see that it's separated into two files that we're going to create, an iterators.php section at the top and a data.csv section at the bottom. First, copy the section for iterators.php, navigate to your code editor, and we'll create a new directory called iterators.
Next, we'll add a new file saved as iterators.php. Paste in our code that we copied from our snippets.txt file. Next, we'll grab the data.csv section. And in our iterators, we'll create a new file and save it as data.cvs. Paste in this code. And we'll save it. Let's first look at data.csv and see exactly what it is.
It's a pretty standard data csv file or a comma separated value file. This means that we have columns of data that's separated by a comma. In this case, we have an ID, a name, note, and extra. Notice each of our rows has some data on that particular row. Pretty simple, pretty basic stuff. Our iterators.php, on the other hand, is a little more complex. This is the meat and potatoes of what we're gonna talk about.
First we create a class, basic iterator, that extends the IteratorIterator class. IteratorIterator is a PHP SPL class returning anything that is traversable, essentially meaning anything that is accessible using a 4H loop into an iterator. This class only implements a single construct method on line four, which takes a path to a file, opens up an SPL file object for it on line six, passes it to the parent construct method, and reads from that SPL file object.
The SPL file object returns a transverable object that we are going to iterate over. At this point, we then pretty much use the iterator. On line 15, we set up a file path. We pass that file path to a new instance of our basic iterator. We take our iterator instance and loop over it using a 4H loop. And then we just var_dump that particular row that we're returning. Recall, at this point, the sequence that is happening. We rewind the iterator. We check the validity of the row.
We then processed a current row. And then call next, move onto the next row, and repeat until we're done looping through our entire iterator. You'll want to save all this and in your terminal, first, change directories to the iterators directory and then run the command php iterators.php. And that was it. We just built and ran our first iterator. Notice a few things here. Because our iterator isn't set up to filter anything, we iterated over every row, even the last row that is actually blank.
For now, though, you can see the basics of our iterator working as we expect. We get an array of outputs, each array being the row in particular that we're outputting. And each element in the array being the value in that particular column in our data file.
Learn how to establish consistency, solve problems, and prevent your applications from crashing by applying the techniques Justin shares in this course. Take your object-oriented programming beyond basic attributes and methods into using constructors, deconstructors, and singletons. Build nested exceptions, use type hints, and explore additional ways you can craft more flexible software using PHP.
- Standard interfaces
- Constructors, deconstructors, and singletons
- Cloning objects
- Abstract classes
- Password hashing and verification
- Type hints, strict type hints, and return types
- Advanced closures
- Nested exceptions and SPL exceptions