5

I am trying to understand what getIterator() is, I will explain:

As I know getIterator is a method we call to include an external Iterator.

The problem is getIterator include it's own methods the closes think looks the same is Iterator interface but it can't be an interface it can be class but i am trying to search it inside the SPL.php source code and didn't find any, maybe I'm making this more complicated than it's really is, I will be happy if some one can help me understand where it is at the SPL.php source code and what is it (class,etc). Thank you all and have a nice day.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
  • **SPL** itself is neither a class nor interface. It's a collection of interfaces and classes so short it's some kind of a library. [PHP SPL](http://www.php.net/manual/en/intro.spl.php). Also `getIterator()` is NOT an internal `PHP` function or method. Are you using an external library? – Robert Wilson Jul 07 '12 at 08:05
  • @ciriusrob OP is refering to `IteratorAggregate::getIterator()` – Gordon Jul 07 '12 at 08:17
  • Thanks @Gordon. I thought he was calling `getIterator()` by it self but not on the `IteratorAggregate` interface. Thanks for the tip. – Robert Wilson Jul 07 '12 at 08:24

1 Answers1

14

ArrayObject implements IteratorAggregate which allows you to return an iterator instead of implementing it. It's pretty straightforward. Assume you have a class wrapping an array, like

class Foo
{
    private $array = [1,2,3,4];
}

and you want to foreach over an instance of Foo, like this:

foreach (new Foo as $bar) {
    echo $bar; // outputs 1234
}

To achieve that you could add the Iterator interface but then you'd had to implement all the methods in the interface for the simple task of iterating the array inside the object. You dont want to duplicate that code over and over again whenever you need that functionality, especially since there already is an Iterator that does what you want. So instead of implementing Iterator you implement IteratorAggregate

class Foo implements IteratorAggregate
{
    private $array = [1,2,3,4];

    public function getIterator()
    {
        return new ArrayIterator($this->array);
    }
}

Now when you do the foreach PHP will use the Iterator returned from getIterator instead of the Foo instance itself allowing you to get the output 1234.

In case of ArrayObject, the point is simply for allowing you to foreach over the values in the ArrayObject. Of course, you can also call getIterator yourself, since it's public and then use returned Iterator directly.

Also note the ctor signature of ArrayObject:

public __construct (
    [ mixed $input 
    [, int $flags = 0 
    [, string $iterator_class = "ArrayIterator" 
]]] )

which notes which Iterator will be returned.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Thank you Gordon, if i got your example right, ArrayIterator constructor is define the the new array and when i am run the the foreach loop the auto built-in behavior using ArrayIterator Methods(current(),key(),next()) for basic foreach loop, the problem is that in this case you were the one to define the method getIterator that using ArrayIterator methods but when looking at ArrayObject class there is only the method getIterator, but we can't take a look at getIterator content, php manual is saying "Retrieve an external iterator" but i want to see it, where doe's this external iterator from? – Aviel Fedida Jul 07 '12 at 10:02
  • @uBlankText not sure what you are looking for, but you can check the implementation of ArrayObject::getIterator here: http://lxr.php.net/xref/PHP_5_4/ext/spl/spl_array.c#1277 – Gordon Jul 07 '12 at 10:15
  • Gordon thank you so much i didn't saw it until now but you were write me the answer at your example all the time, i was looking at getIterator and didnt understand that your example is the content of the method from the arrayobject class, i am really sorry, now i take a loook at this link:http://www.php.net/~helly/php/ext/spl/classArrayObject.html#c1c281aed693a0e74e412a2a7a94015b and now all its much straightforward thank you and have a nice day. – Aviel Fedida Jul 07 '12 at 11:09