 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Jake Smith's Blog: Callback Filter Iterator in PHP 5.3/5.4
by Chris Cornutt December 02, 2011 @ 08:44:34
Jake Smith has a new post to his blog today about a feature included in PHP's Standard PHP Library that you might have overlooked - the FilterIterator's callback functionality.
The Filter Iterator is probably my second favorite iterator, next to Directory Iterator. There are many great use cases for the Filter Iterator, and when you do filter the original data is left untouched. A Filter Iterator is really simple to use, create a class that extends FilterIterator and adjust the accept method to meet your criteria. This is great and all, but having the ability to create filter iterators on the fly, ones that won't be used application wide, without having to create a class is even better.
He includes a bit of code defining a FilterCallbackIterator class with a "callback" parameter passed into the constructor (in his case, a closure). Also included is some sample code of it in use - handling an array (well, ArrayIterator) with a simple true/false check on the current array value. You can find out more about this functionality in the PHP manual.
voice your opinion now!
callback filter iterator spl tutorial closure
DevShed: Implementing the ArrayAccess Interface - PHP
by Chris Cornutt June 17, 2011 @ 08:58:04
In the first part of a new series over on DevShed.com, they introduce the concept of "segregated interfaces" and show how to use them to work with collections of arrays (using interfaces that are a part of the SPL).
To start illustrating why segregated interfaces are really useful, in the lines to come I'm going to build an example that will recreate the scenario described in the introduction. Basically, what I want to achieve here is to construct a custom countable array collection.
He shows the basic class structure needed to emulate a countable array in an object by implementing the "Countable" interface. He adds in the "Iterator" interface to allow you to work with the dataset like an array - progressing through it, rewinding to the beginning and checking to see if a value exists. Finally, they add the "ArrayAccess" interface to the class that boosts it with even more features like the ability to grab things by specific keys (numeric or string). The finish the article off with an example of an ArrayCollection object and how it can be looped through using a foreach.
voice your opinion now!
tutorial arrayaccess interface countable iterator spl
Web Species Blog: Lazy evaluation with PHP
by Chris Cornutt June 01, 2011 @ 08:41:01
Juozas Kaziukenas has a new post to his Web Species blog about using "lazy evaluation" in PHP - loading the resources you need for execution and evaluation only as you need them, not all up front.
Recently I needed to process a huge array of data and because of PHP's somewhat inefficient variables and especially arrays that was resulting in "out of memory" errors. However, I couldn't use any other tools than PHP so was forced to come up with a solution implementation in it. Here is how I solved it using principles from functional languages.
He gives an example using Haskell to generate a Fibonacci sequence using its built-in lazy evaluation abilities. Unfortunately, PHP doesn't have such a thing built in, so he tries the next best thing - Iterators. He caries the idea over to the database side too, recommending fetch() in a loop over fetchAll() and some effective joins.
voice your opinion now!
lazy evaluation haskell functional iterator
SitePoint PHP Blog: Simple Object Iterators in PHP
by Chris Cornutt April 28, 2011 @ 12:15:10
On the SitePoint PHP blog today there's a new post from Craig Buckler looking (quickly) at a handy feature of the Standard PHP Library (SPL) that can help make working with objects and arrays simpler - object iterators.
If you've been coding in PHP for a while, you may be familiar with the foreach loop. It provides an easy way to analyze every item in an array. As well as arrays, it's also possible loop through an object. If your object contains a collection of items, you can use a foreach loop to iterate over each of them. [...] Iterators is a subject which strikes fear into the heart of many developers. They sound complex and are often explained with indecipherable abstract references.
To help combat this impression, he gives a bit more practical example - turning a regular array into an ArrayIterator and looping through the collection as an object instead of just a variable. This helps to keep things contained.
voice your opinion now!
object iterator array spl tutorial
Christian Schaefer's Blog: Simply iterate over XML with plain PHP using little memory and CPU
by Chris Cornutt March 10, 2011 @ 08:11:31
In a new post to his Test.ical.ly blog Christian Schaefer shows you how to iterate over XML in a more efficient way with the help of the XMLReader and Iterator features that come with PHP.
One of the things I have been working on lately was a simple XML parser. It's a simple XML structure in my case though it could be more complex without much change. My solution was a quite powerful yet simple combination of XMLReader and the Iterator interface.
He includes a sample XML document similar to the one he was working with and shows how XMLReader can handle it, keeping only the currently needed information in memory at one time. His sample class (CustomXml) loads the file and defines all of the iterator methods to work with the data like "next", "prev" and "rewind".
voice your opinion now!
iterate xml xmlreader iterator memory tutorial
Kevin Schroeder's Blog: You want to do WHAT with PHP? Chapter 5
by Chris Cornutt September 03, 2010 @ 11:14:58
Kevin Schroeder has the latest excerpt from his book posted ("You Want to Do WHAT with PHP?") with a focus on the SPL - Standard PHP Library. He specifically takes a quick look at Iterators.
If you are doing any data processing whatsoever you are using arrays. And most likely you are doing database queries, iterating over the results and doing your algorithm-ing. But what if you have additional functionality that you need to have integrated with your data. You could go the traditional route and copy and paste half your application around or you could build, what we like to call structured applications. SPL allows you to do that.
In the excerpt he mentions the methods that come built in to Iterators like rewind, current and valid. There's also some sample code showing a custom iterator that works with some user data to output the information set in the constructor. You can find out more about this chapter and the rest in Kevin's book.
voice your opinion now!
book excerpt kevinschroeder iterator spl standard library
Matthew Weier O'Phinney's Blog: Applying FilterIterator to Directory Iteration
by Chris Cornutt August 17, 2010 @ 10:29:38
New on his blog Matthew Weier O'Phinney has this post looking about using the FilterIterator (from PHP's SPL libraries) to work with (recursive) directory iteration.
I'm currently doing research and prototyping for autoloading alternatives in Zend Framework 2.0. One approach I'm looking at involves creating explicit class/file maps; these tend to be much faster than using the include_path, but do require some additional setup. [...] I'm well aware of RecursiveDirectoryIterator, and planned to use that. However, I also had heard of FilterIterator, and wondered if I could tie that in somehow. In the end, I could, but the solution was non-obvious.
He starts with what he thought he should be able to do with the FilterIterator - pass in a DirectoryIterator to be able to filter them recursively. Unfortunately this only worked for the first level, so he looked else where. His solution ultimately involved passing in a RecursiveIteratorIterator instance into the DirectoryIterator that contained his RecursiveDirectoryIterator. He includes a full code example in the post showing how to locate a certain file/class recursively inside a directory.
voice your opinion now!
iterator spl filteriterator directory tutorial
Sean Coates' Blog: A Case of Mistaken Iterator
by Chris Cornutt July 29, 2010 @ 12:48:51
In a new post to his blog today Sean Coates talks about some of his work with Iterators in PHP and how, despite a bad example in the manual, he solved his issue (and updated the PHP manual too).
In the back end, we have models that connect to CouchDB. These models implement the Iterator pattern to allow easy traversal of a record's keys. [...] Little did I realize that this implementation is very broken. [...] Over the past few years, I've implemented many iterators in this way, using PHP's implicit array manipulation functions (reset(), current(), key(), next()).
He points out some issues with how PHP handles array index tracking and how, in the previous PHP manual example, it incorrectly checked for "false" against the current array value. His updated version doesn't have this issue. You can see it here.
voice your opinion now!
iterator mistake manual update
|
Community Events
Don't see your event here? Let us know!
|