Je travaille actuellement sur un projet et j'ai une classe qui implémente l'interface ArrayAccess.PHP 5.3 et l'interface ArrayAccess
Howewer, je reçois une erreur qui dit que ma mise en œuvre:
doit être compatible avec celui de ArrayAccess :: offsetSet().
Ma mise en œuvre ressemble à ceci:
public function offsetSet($offset, $value) {
if (!is_string($offset)) {
throw new \LogicException("...");
}
$this->params[$offset] = $value;
}
Alors, pour moi, il semble que ma mise en œuvre est correcte. Une idée de ce qui ne va pas? Merci beaucoup!
Le look classe comme ceci:
class HttpRequest implements \ArrayAccess {
// tons of private variables, methods for working
// with current http request etc. Really nothing that
// could interfere with that interface.
// ArrayAccess implementation
public function offsetExists($offset) {
return isset ($this->params[$offset]);
}
public function offsetGet($offset) {
return isset ($this->params[$offset]) ? $this->params[$offset] : NULL;
}
public function offsetSet($offset, $value) {
if (!is_string($offset)) {
throw new \LogicException("You can only assing to params using specified key.");
}
$this->params[$offset] = $value;
}
public function offsetUnset($offset) {
unset ($this->params[$offset]);
}
}
Le look classe comme ceci:
class HttpRequest implements \ArrayAccess {
// tons of private variables, methods for working
// with current http request etc. Really nothing that
// could interfere with that interface.
// ArrayAccess implementation
public function offsetExists($offset) {
return isset ($this->params[$offset]);
}
public function offsetGet($offset) {
return isset ($this->params[$offset]) ? $this->params[$offset] : NULL;
}
public function offsetSet($offset, $value) {
if (!is_string($offset)) {
throw new \LogicException("You can only assing to params using specified key.");
}
$this->params[$offset] = $value;
}
public function offsetUnset($offset) {
unset ($this->params[$offset]);
}
}
quelle ligne est de lancer l'erreur? – user103219
La ligne avec la déclaration de classe (la classe X implémente \ ArrayAccess). –
Cela peut être utile si vous fournissez le reste de la classe (ou une version raccourcie). –