Manuel PHP

The ArrayAccess interface

Introduction

Interface to provide accessing objects as arrays.

Synopsis de la classe

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

Exemple #1 Basic usage

  1. <?php
  2. class obj implements arrayaccess { 
  3.    private $container = array(); 
  4.    public function __construct() { 
  5.       $this->container = array( 
  6.          "one" => 1, 
  7.          "two" => 2, 
  8.          "three" => 3, 
  9.       ); 
  10.    } 
  11.    public function offsetSet($offset, $value) { 
  12.       $this->container[$offset] = $value; 
  13.    } 
  14.    public function offsetExists($offset) { 
  15.       return isset($this->container[$offset]); 
  16.    } 
  17.    public function offsetUnset($offset) { 
  18.       unset($this->container[$offset]); 
  19.    } 
  20.    public function offsetGet($offset) { 
  21.       return isset($this->container[$offset]) ? $this->container[$offset] : null; 
  22.    }  
  23. }  
  24.  
  25. $obj = new obj;  
  26.  
  27. var_dump(isset($obj["two"]));  
  28. var_dump($obj["two"]);  
  29. unset($obj["two"]);  
  30. var_dump(isset($obj["two"]));  
  31. $obj["two"] = "A value";  
  32. var_dump($obj["two"]);  
  33.  
  34. ?> 

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

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

Sommaire


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