This article mainly introduced the PHP aggregation iterator interface iteratoraggregate usage, combined with the instance form analysis the aggregation iterator interface iteratoraggregate The concept, the function, the definition and the use method, the need friend can refer to the next
The example in this paper describes the PHP aggregation iterator interface iteratoraggregate usage. Share to everyone for your reference, as follows:
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 through foreach, specifically referring to the SPL iterator, and interested friends can focus on the PHP Chinese web.
Articles you may be interested in:
PHP Detection Interface traversable use of detailed
PHP Custom Serialization interface serializable usage analysis explained
PHP Opcache accelerated use of the method of explanation