
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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
- Related Articles
- PHP ArrayAcccess interface
- PHP Iterable interface
- PHP Serializable interface
- PHP Throwable interface
- PHP Traversable interface
- Explain interface in PHP.
- What is Stringable Interface in PHP 8?
- Why an interface cannot implement another interface in Java?
- What is the user interface and operating system interface?
- Interface in Java
- Interface in C#
- C# Interface Types
- Network Interface Device
- Multiset Interface – Java
- Java Interface methods

Advertisements