0

So I lean back all relaxed and sh*t, refurbishing my design patterns knowledge, everything going fine and dandy, as I wait for the weekend adrenaline to kick in. My life couldn't get any smoother. Next thing you know... I get the biggest realization of my life day.

Why do we need Iterator Pattern? Or better phrased as

Do we even need Iterator Pattern in today's age?

Since I'm a native PHP speaker. No-brainer, I was going through this official DOC.

So through various Rube Goldberg-ish examples in the comments, the pattern goes on to create various objects passing an array to them, only to eventually iterate over the items using foreach() or a while() so my question is where in the grand scheme of things does this pattern fit in, Or couldn't we have been better off just iterating over the array itself in the first place or as always ....

Am I missing something?

Example:

<?php
class myIterator implements Iterator 
    {
    private 
        $_array = array();
    public function __construct(array $array) 
        {
        $this->_array = $array;
        }
    public function rewind() 
        {
        reset($this->_array);
        }
    public function current() 
        {
        return current($this->_array);
        }
    public function key() 
        {
        return key($this->_array);
        }
    public function next() 
        {
        next($this->_array);
        }
    public function valid() 
        {
        return $this->key() !== null;
        }
    }

$it = new myIterator(array('foo_1' => 'bar_1','foo_2' => 'bar_2'));

//example 1 : foreach

foreach($it as $key => $value) 
    {
    var_dump($key, $value);
    }

//example 2 : while

$it -> rewind();
while($it->valid())
    {
    var_dump($it->key(), $it->current());

    $it->next();
    }

//example 3 : for

for($it->rewind();$it->valid();$it->next())
    {
   var_dump($it->key(), $it->current());
    }
?>
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • 2
    I don't really understand what you're hoping for in an answer, how are you to apply logic to and/or traverse a set of `thing` without an iterator? – Kritner Dec 16 '16 at 14:12
  • 4
    Just because your example is an iterator to iterate over something that's already iterable anyway, doesn't mean that every iterator is intended to iterate over an existing iterable, but perhaps consider that it might be making something non-iterable iterable – Mark Baker Dec 16 '16 at 14:23
  • Please stop `mollycoddling` this answer by editing it, its phrased to precisely convey my emotions.!!! – Mohd Abdul Mujib Dec 16 '16 at 14:23
  • @MarkBaker Whoa!!!! Thats one supercool tongue twister right there!!!! Btw, any reference/pointer to some resource for an example which tries to convert non-iterable into iterable? Please? – Mohd Abdul Mujib Dec 16 '16 at 14:26
  • @Kritner I am hoping for a great answer which succinctly defines the iterator pattern, provides a nice and simple example which explains which scenarios it can be useful. – Mohd Abdul Mujib Dec 16 '16 at 14:35
  • 1
    [PHP - Reasons to use Iterators?](http://stackoverflow.com/questions/6247066/php-reasons-to-use-iterators) – WEBjuju Dec 16 '16 at 14:36
  • @WEBjuju Thanks, owing to your awesome Google-Fu skillz, I can now relax sipping pineapple mocktail. #VotedToClose :-* – Mohd Abdul Mujib Dec 16 '16 at 14:39
  • 1
    One perfect example would be iterating through a "tree" of objects, such as a b-tree, which isn't inherently iterable – Mark Baker Dec 16 '16 at 15:04

0 Answers0