This article mainly introduces the PHP aggregation iterator interface iteratoraggregate usage, combined with the example form analysis the aggregation iterator interface Iteratoraggregate concept, function, definition and use method, need friend can refer to, hope can help everybody.
PHP Iteratoraggregate, also known as a converged iterator, provides an interface for creating an external iterator, which is summarized as follows:
Iteratoraggregate extends traversable { abstract public traversable getiterator (void)}
When implementing the Getiterator method, you must return an instance of the class that implements the iterator interface.
Example Description:
<?php/** * leverages the aggregation iterator and returns an instance of the class that implements the iterator interface * * @author crazy old driver */class MyData implements iteratoraggregate {public $one = "Public Property One"; Public $two = "Public property"; Public $three = "Public Property three"; Public Function __construct () { $this->last = ' last property '; } Public Function Getiterator () { return new arrayiterator ($this);} } $obj = new Mydata;foreach ($obj as $key = = $value) { var_dump ($key, $value); Echo ' <br> ';//Linux:echo "\ n";}? >
The above example outputs:
String ' One ' (length=3) string ' Public property One ' (length=19) string ' "(length=3) String ' public property ' (length =19) string ' three ' (length=5) string ' Public property three ' (length=21) string ' last ' (length=4) string ' Last property ' (Le NGTH=13)
The
Arrayiterator iterator encapsulates an object or array as a class that can be manipulated by using foreach, which can be described in detail in the SPL iterator.