L'interface ArrayAccess

Introduction

Interface permettant d'acc�der aux objets de la m�me fa�on que pour les tableaux.

Sommaire de l'Interface

ArrayAccess
ArrayAccess {
/* M�thodes */
abstract public boolean offsetExists ( string $offset )
abstract public mixed offsetGet ( string $offset )
abstract public void offsetSet ( string $offset , string $value )
abstract public void offsetUnset ( string $offset )
}

Exemple #1 Exemple simple

<?php
class obj implements arrayaccess {
    private 
$container = array();
    public function 
__construct() {
        
$this->container = array(
            
"un"   => 1,
            
"deux"   => 2,
            
"trois" => 3,
        );
    }
    public function 
offsetSet($offset$value) {
        
$this->container[$offset] = $value;
    }
    public function 
offsetExists($offset) {
        return isset(
$this->container[$offset]);
    }
    public function 
offsetUnset($offset) {
        unset(
$this->container[$offset]);
    }
    public function 
offsetGet($offset) {
        return isset(
$this->container[$offset]) ? $this->container[$offset] : null;
    }
}

$obj = new obj;

var_dump(isset($obj["deux"]));
var_dump($obj["deux"]);
unset(
$obj["deux"]);
var_dump(isset($obj["deux"]));
$obj["deux"] = "Une valeur";
var_dump($obj["deux"]);

?>

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

bool(true)
int(2)
bool(false)
string(7) "A value"

Sommaire