Reputation: 62366
I stumbled onto this PHP: Iterators page today and am wondering why these classes are even needed. I'd like to make the assumption that they serve a purpose, otherwise they wouldn't be in PHP. I'm struggling to see the benefit of such items when there are already very simple ways of doing these.
Is PHP making an error to be a more respectable/object oriented programming language? Or is there really a benefit to doing this?
A good example of how there's 2 ways of doing this was found in a PHP comment on the ArrayIterator:
<?php
$fruits = array(
"apple" => "yummy",
"orange" => "ah ya, nice",
"grape" => "wow, I love it!",
"plum" => "nah, not me"
);
$obj = new ArrayObject( $fruits );
$it = $obj->getIterator();
// How many items are we iterating over?
echo "Iterating over: " . $obj->count() . " values\n";
// Iterate over the values in the ArrayObject:
while( $it->valid() ) {
echo $it->key() . "=" . $it->current() . "\n";
$it->next();
}
// The good thing here is that it can be iterated with foreach loop
foreach ($it as $key=>$val)
echo $key.":".$val."\n";
/* Outputs something like */
Iterating over: 4 values
apple=yummy
orange=ah ya, nice
grape=wow, I love it!
plum=nah, not me
?>
Upvotes: 1
Views: 200
Reputation: 1538
Iterators are included in PHP because it lets you use common language constructs (such as foreach) with arbitrary objects, instead of being restricted to looping over built-in objects like arrays. It's a hassle (and breaks encapsulation) to force an object to transform it's internal state into an array for you to iterate over, and for large datasets can make your system run out of memory. An iterable lets you get past both of those issues by letting the object itself return individual elements only when requested.
Upvotes: 5
Reputation: 17457
You may come across an instance where you wish to implement the IteratorAggregate into a class, so you can iterator over a private member without having to make it public, or create a public function:
class BaseballTeams implements IteratorAggregate
{
private $teams;
public function __construct( )
{
$this->teams = explode( ',', "Tigers,Cubs,Orioles,Mariners,Yankees,Blue Jays,Marlins" );
}
public function getIterator( )
{
return new ArrayIterator( $this->teams );
}
}
Now, you can loop through the private $teams member, like so:
$baseball = new BaseballTeams( );
foreach ( $baseball as $n => $t )
{
echo "<p>Team #$n: $t</p>";
}
Upvotes: 0
Reputation: 1051
For my example I won't limit the answer to Iterator but also include Traversable.
Take a look at SimpleXML (SimpleXMLElement and SimpleXMLIterator).
Highly useful and easy to use and allows you to convert your XML into an object which can be iterated over/traversed through simply because it extended the classes that provide that functionality. I have foreach'd my way through a fair share of XML and am pretty glad it didnt have to reinvent the wheel to provide that. I am sure there are many more examples of their use and reason for existing but these were the first that popped up that I have immediately benefited from that aren't arrays.
Upvotes: 1
Reputation: 27295
Your example is not really the why to use Iterator objects. In most ways the Iterator object is used to iterate over a class and use a class direct as an iterator.
http://php.net/manual/de/class.iterator.php
here is a short example.
When you have a single array its really easier to make a foreach over the array.
Upvotes: 1