2010-02-13 77 views
0

我现在正在研究一个项目,并且我有一个实现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]); 
    } 
} 
+0

哪一行抛出的错误? – user103219 2010-02-13 13:30:31

+0

具有类声明的行(类X实现\ ArrayAccess)。 – 2010-02-13 13:33:36

+0

如果您提供课程的其余部分(或缩写版本),它可能会有所帮助。 – 2010-02-13 13:33:51

回答

0

映入这里我的眼睛的唯一的事:

public function offsetGet($offset) { 
    return isset ($this->params[$offset]) ? $this->params[$offset] : NULL; 
    } 

也许将其替换为:

public function offsetGet($offset) { 
    return (isset ($this->params[$offset]) ? $this->params[$offset] : NULL); 
    } 

会得到诀窍。

它也可能是一个语法错误,从你没有粘贴的代码部分拖动。

+0

谢谢,但虽然这肯定是正确的,它提高了代码的可读性,但它并没有帮助我解决这个问题。 – 2010-02-13 13:59:13

+0

这个语法错误的好主意,但在删除实现后,类再次完美地工作。 – 2010-02-13 14:01:45

+0

你的代码在这里工作正常。将其粘贴到文件中,并使用以下命令从命令行运行它:php test.php。它必须是你没有粘贴的代码的一部分。 – 2010-02-13 14:04:27

1

在我看来,像你的namespaceuse指令在文件的顶部使它看起来与错误的ArrayAccess接口兼容。虽然没有这些指令,但是无法确定。

一般:

你自己的命名空间不应该开始或以反斜杠结尾:

用途: namespace Web\Http;

不要使用类似: namespace \Web\Http;namespace \Web\Http\;

对于每一个类和接口你在文件中引用,添加一个use指令:

namespace MyProject; 

use MyLibrary\BaseClass; // note no backslash before the namespace name 
use \ArrayAccess; 
use \Iterator; 
use \Countable; 

class MyClass extends BaseClass implements ArrayAccess, Iterator, Countable 
{ 
    /* Your implementation goes here as normal */ 
}