1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php
class tableau implements ArrayAccess {
private $tableau = array() ;
function offsetExists( $index ) {
return isset( $this->tableau[$index] ) ;
}
function &offsetGet( $index ) {
// Notez le & devant le nom de la fonction
return $this->tableau[$index] ;
}
function offsetSet( $index, $valeur ) {
return $this->tableau[ $index ] = $valeur ;
}
function offsetUnset( $index ) {
unset( $this->tableau[ $index ] ) ;
}
}
$tab = new tableau() ;
if ( !isset($tab[42]) ) {
$tab[42] = 1 ;
}
echo ++$tab[42] ; // Affiche 2
unset( $tab[42] ) ; |
Partager