我现在正在研究一个项目,并且我有一个实现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