background image

public

 

function

 next() 

$this

->_index++; 



 

$array

 = 

array

('A', 'B', 'C', 'D'); 

echo

 "Collection: "; 

foreach

 (

new

 Collection(

$array

as

 

$key

 => 

$value

) { 

echo

 "$key => $value. "; 

echo

 "\n"; 

/** 
* Usually IteratorAggregate is the interface to implement. 
* It has only one method, which must return an Iterator 
* already defined as another class (e.g. ArrayIterator) 
* Iterator gives a finer control over the algorithm, 
* because all the hook points of Iterator' contract 
* are available for implementation. 
*/

 

class

 NumbersSet 

implements

 IteratorAggregate 

private

 

$_content

 

public

 

function

 __construct(

array

 

$content

$this

->_content = 

$content


 

public

 

function

 contains(

$number

return

 in_array(

$number

$this

->_content); 


 

/** 
* Only this method is necessary to implement IteratorAggregate. 
* @return Iterator 
*/

 

public

 

function

 getIterator() 

return

 

new

 ArrayIterator(

$this

->_content);