Can we use IterateAggregate or Iterator in foreach loop in php?

I start with php and learn it with php.net. A note on this page ( http://php.net/manual/en/class.traversable.php ) says that:

Internal (built-in) classes that implement this interface can be used in the foreach construct and do not need to implement IteratorAggregate or Iterator.

What does this note say? Does this mean that we can use IteratorAggregate or Iterator inside the foreach loop without any class, or maybe I'm wrong. Can anyone say what this note says?

+5
source share
1 answer

IteratorAggregate is an interface for creating an external Iterator that allows you to navigate through a custom class of objects using foreach :

 class FooBarClass implements IteratorAggregate { public $property = "Nothing to see"; public function __construct() { } public function getIterator() { return new ArrayIterator($this); } } $obj = new FooBar; foreach($obj as $key => $value) { print(var_dump($key, $value) . "\n"); } 

Iterator also an interface for external iterators, but allows your classes or objects to perform internal iteration:

 class myIterator implements Iterator { private $position = 0; private $array = array('one', 'two', 'three'); function rewind() { $this->position = 0; } function current() { return $this->array[$this->position]; } function key() { return $this->position; } function next() { ++$this->position; } function valid() { return isset($this->array[$this->position]); } } 

However, you can still move objects in the same way as with IteratorAggregate .

The difference between the two is that IteratorAggregate easier to implement than Iterator, and usually faster. The disadvantage is that it is easy to bypass and does not provide the following methods (), key (), etc., since they are not called during foreach traversal.

While Iterator (or more specific OuterIterator or (simpler) IteratorIterator ) allows you to significantly better control iteration through your object and add custom exceptions for the following errors next (), key () or prev (), caching (), etc. d.

The note you have in mind means that some PHP inner classes (written in C code) can directly implement this interface. Any custom class that must implement Traversable must do this by running IteratorAggregate or Iterator or the other to go down with Traversable. See Pro PHP by Kevin MacArthur . 143f.

The Traversable interface is an abstract base interface (without methods, as shown in the brief description of the interface) that cannot be created. However, it can be used to check if a class is accessible using foreach or not.

 Traversable { } 

Part of the confusion is objects, and arrays do not implement "Traversable", but can be traversed through foreach, but you cannot check foreach compatibility with the help of instanceof or.

 $myarray = array('one', 'two', 'three'); $myobj = (object)$myarray; if ( !($myarray instanceof \Traversable) ) {  print "myarray is NOT Traversable"; } if ( !($myobj instanceof \Traversable) ) {  print "myobj is NOT Traversable"; } 

As mentioned earlier, each object can be moved using foreach, but then you can just access the public properties. Quoting from the PHP manual on Iterating Objects :

PHP 5 provides a way to define objects so that you can iterate over a list of elements using, for example, a foreach expression. By default, all visible properties will be used for iteration.

So, if you encapsulate an object with private and protected values โ€‹โ€‹and write getter and setter methods to access it, you might like your class to implement IteratorAggregate or Iterator and write logic to make these values โ€‹โ€‹available in foreach loops as needed.

In short, objects that implement the Traversable interface (via Iterator or IteratorAggregate) "act like an array." However, this is not necessary for iterating the object. But you need to implement Iterator if you want to change their behavior. The same logic applies to inline classes.

+2
source

Source: https://habr.com/ru/post/1272210/


All Articles