L'interface IteratorAggregate

(PHP 5, PHP 7)

Introduction

Interface pour créer un itérateur externe.

Sommaire de l'Interface

IteratorAggregate extends Traversable {
/* Méthodes */
abstract public getIterator ( void ) : Traversable
}

Exemple #1 Exemple simple

<?php
class myData implements IteratorAggregate {
    public 
$property1 "Propriété publique numéro un";
    public 
$property2 "Propriété publique numéro deux";
    public 
$property3 "Propriété publique numéro trois";

    public function 
__construct() {
        
$this->property4 "dernière propriété";
    }

    public function 
getIterator() {
        return new 
ArrayIterator($this);
    }
}

$obj = new myData;

foreach(
$obj as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>

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

string(9) "property1"
string(19) "Propriété publique numéro un"

string(9) "property2"
string(19) "Propriété publique numéro deux"

string(9) "property3"
string(21) "Propriété publique numéro trois"

string(9) "property4"
string(13) "dernière propriété"

Sommaire