【问题标题】:PHP 5.3 and interface \ArrayAccessPHP 5.3 和接口 \ArrayAccess
【发布时间】:2010-02-13 13:25:04
【问题描述】:

我现在正在做一个项目,我有一个实现 ArrayAccess 接口的类。

但是,我收到一个错误,说明我的实现:

必须与 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]);
  }
}

【问题讨论】:

  • 哪一行报错了?
  • 类声明行(X类实现\ArrayAccess)。
  • 如果您提供课程的其余部分(或课程的缩短版),可能会有所帮助。
  • 如果你停止它抛出异常会发生什么?
  • 你的类声明在命名空间内吗?例如你在文件顶部有namespace My\Namespace; 声明吗?如果没有,您不需要限定 ArrayAccess。您也可以尝试“使用”ArrayAccess 而不是完全限定它。这似乎是一个奇怪的错误,所以我只是把东西扔在那里......

标签: arrays php


【解决方案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 */
}

【讨论】:

    【解决方案2】:

    这里唯一吸引我的地方:

     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);
      }
    

    会成功的。

    这也可能是您未粘贴的代码部分的语法错误。

    【讨论】:

    • 谢谢,虽然这肯定是正确的并且提高了代码的可读性,但它并不能帮助我解决这个问题。
    • 语法错误的好主意,但删除实现后,该类再次完美运行。
    • 您的代码在这里运行良好。将其粘贴到一个文件中并使用以下命令从命令行运行它:php test.php。它必须是您尚未粘贴的代码部分中的某些内容。
    • 您好,您使用的是什么版本的 PHP? PHP 5.3.X?
    • 我刚刚做了同样的事情,将实现注释掉了,它工作了。上面的代码很好。反正应该没问题。我不是说 Netbeans 6.8 IDE 完全可靠,但它不会报告任何语法错误。
    猜你喜欢
    • 2012-07-05
    • 2010-12-05
    • 2011-05-18
    • 2014-03-01
    • 2014-03-16
    • 2012-02-05
    • 2015-01-22
    • 2015-06-10
    • 2018-03-18
    相关资源
    最近更新 更多