Le code suivant issue d'un exemple du bouquin PHP5 concernant l'auto-incrementation
avec l'interface ArrayAcess.
<?php
class tableau implements ArrayAccess{
private $tableau = array();
function offsetExists($index){
return isset($this->tableau[$index]);
}
function &offsetGet($index){
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]);
?>
M'affiche l'erreur suivante :Code : Tout sélectionner
Fatal error: Declaration of tableau::offsetGet() must be compatible with that of ArrayAccess::offsetGet() avec l'interface ArrayAccess.
Pourtant le livre dit que c'est possible , alors d'ou vient le problème ?
Merci d'avance.
Ce que l'on apprend par l'effort reste toujours ancré beaucoup plus longtemps.