ArrayObject, getIterator ();

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

As I know, getIteratorthis is a method that we call to enable an external iterator.

The problem is that getIterator includes its own methods . It seems that the closure looks the same as the Iterator interface, but it may not be an interface, which may be a class, but I'm trying to find it inside the SPL. php and did not find, maybe I am making it more complicated than it really is, I will be glad if someone can help me understand where it is in the source code of SPL.php and what it is (class, and t .d). Thank you all and have a nice day.

+5
source share
1 answer

ArrayObject IteratorAggregate, . . , , ,

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

foreach Foo, :

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

Iterator, . , , , , , . Iterator IteratorAggregate

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

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

, foreach, PHP Iterator, getIterator Foo, 1234.

ArrayObject foreach ArrayObject. , getIterator , , Iterator .

ctor- ArrayObject:

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

, .

+12

All Articles