
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP IteratorAggregate Interface
Introduction
IteratorAggregate interface extends abstract Traversable interface. It is implemented by a class to create external iterator. This interface introduces on abstract method called getIterator.
Syntax
IteratorAggregate extends Traversable { /* Methods */ abstract public getIterator ( void ) : Traversable }
Methods
IteratorAggregate::getIterator — Retrieve an external iterator
This function has no parameters and returns an instance of an object implementing Iterator or Traversable.
IteratorAggregate Example
In following PHP script, a class that implements IteratorAggregate interface contains an array as a propertyThe getIterator() method returns ArrayIterator object out of this array. We can traverse the array using foreach loop.
Example
<?php class myIterator implements IteratorAggregate { public $arr; public function __construct() { $this->arr = array(10,20,30,40); } public function getIterator() { return new ArrayIterator($this->arr); } } $obj = new myIterator(); foreach($obj as $key => $value) { echo $key ." =>" . $value . "
"; } ?>
Output
traversal of array property shows following result
0=>10 1=>20 2=>30 3=>40
Advertisements