PHP Iterator classes
I'm trying to figure out what's the actual benefit of using Iterator
classes in Object Oriented PHP over the standard array.
I'm planning to upgrade my framework by converting all arrays to object,
but I just don't understand the actual need apart from having the system
being fully OOP.
I know that by the use of IteratorAggregate I can create:
class MyModel implements IteratorAggregate {
public $records = array();
public function __construct(array $records) {
$this->records = $records;
}
public function getIterator() {
return new ArrayIterator($this->records);
}
}
and then simply loop through it like using the array:
$mdlMy = new MyModel();
foreach($mdlMy as $row) {
echo $row['first_name'];
echo $row['last_name'];
}
Could someone in simple terms explain the actual purpose of these -
perhaps with some use case.
No comments:
Post a Comment