我現在正在研究一個項目,並且我有一個實現ArrayAccess接口的類。PHP 5.3和interface ArrayAccess
Howewer,我收到說,我實現了一個錯誤:
必須與ArrayAccess接口兼容:: offsetSet()。
我的實現看起來是這樣的:
public function offsetSet($offset, $value) {
if (!is_string($offset)) {
throw new \LogicException("...");
}
$this->params[$offset] = $value;
}
所以,對我來說,它看起來像我的實現是正確的。任何想法有什麼不對?非常感謝!
類是這樣的:
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]);
}
}
類是這樣的:
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]);
}
}
哪一行拋出的錯誤? – user103219 2010-02-13 13:30:31
具有類聲明的行(類X實現\ ArrayAccess)。 – 2010-02-13 13:33:36
如果您提供課程的其餘部分(或縮寫版本),它可能會有所幫助。 – 2010-02-13 13:33:51