About PHP converged iterator interface iteratoraggregate usage sharing

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.