In most cases the implementation of external iterators in PHP takes place when it becomes necessary to build wrappers around database result sets and plain arrays. However, they can be of great help when it comes to sticking with the DRY (Don’t Repeat Yourself) and the Single Responsibility principles as well. By using an outer element that encapsulates an intuitive API and all the functionality required for iterating over common data structures like the ones mentioned above, it is possible to build different types of traversable classes without having to pollute them with unnecessary responsibilities. To demonstrate how to translate these seemingly-complex theoretical concepts into concrete and functional code, in earlier chapters of this series I developed a few examples which showed how to use external iterators; first for traversing the protected fields of an entity modeling class and secondly for constructing a file helper, which had the ability for lazy-loading data from a sample text file. This last example is especially interesting, as its driving logic can be adapted to work with other types of persistence mechanisms, such a RDBMS, a web service or a CDN. In addition, it’s valid to point out that the aforementioned file helper made use of a custom array collection to fetch (on request) data from a specified target file. Considering that the data was converted to an array prior to being processed, it’d be really easy to refactor the helper and make it work with the native ArrayIterator SPL class. That would mean writing less code, while maintaining the same functionality. A hard-to-beat deal, indeed. In line with this idea, in this final installment of the series I’m going to show you how simple is to refactor the pertinent file helper, so that it can employ the built-in ArrayIterator class for lazy-loading file data, instead of appealing to a custom array collection. Recap Time: Lazy-loading File Data Using a Custom External Iterator In case that you still haven’t had the chance to read the preceding tutorials in this series, I will recap the source code corresponding to this example, so that you can dissect it and grasp its driving logic. With that being said, here’s an implementation of the pertaining external iterator. Take a look:
As you can see, the “ArrayCollection” class is nothing but a generic iterator, which enables it to traverse, count and access the elements stored in its protected $_data field by using an array-like notation.
blog comments powered by Disqus |