2010-02-13 38 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 */ 
}