Manuel PHP

The IteratorAggregate interface

Introduction

Interface to create an external Iterator.

Synopsis de la classe

IteratorAggregate
IteratorAggregate implements Traversable {
/* Methods */
abstract public Traversable IteratorAggregate::getIterator ( void )
}

Exemple #1 Basic usage

  1. <?php
  2. class myData implements IteratorAggregate { 
  3.    public $property1 = "Public property one"; 
  4.    public $property2 = "Public property two"; 
  5.    public $property3 = "Public property three"; 
  6.  
  7.    public function __construct() { 
  8.       $this->property4 = "last property"; 
  9.    } 
  10.  
  11.    public function getIterator() { 
  12.       return new ArrayIterator($this); 
  13.    }  
  14. }  
  15.  
  16. $obj = new myData;  
  17.  
  18. foreach($obj as $key => $value) { 
  19.    var_dump($key, $value); 
  20.    echo "\n";  
  21. }  
  22. ?> 

L'exemple ci-dessus va afficher quelque chose de similaire à :

string(9) "property1" string(19) "Public property one" string(9) "property2" string(19) "Public property two" string(9) "property3" string(21) "Public property three" string(9) "property4" string(13) "last property"

Sommaire


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique