Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PHP 5.3和接口\ArrayAccess_Arrays_Php - Fatal编程技术网

Arrays PHP 5.3和接口\ArrayAccess

Arrays PHP 5.3和接口\ArrayAccess,arrays,php,Arrays,Php,我现在正在从事一个项目,我有一个类实现ArrayAccess接口 然而,我得到一个错误,说我的实现: 必须与ArrayAccess::offsetSet()的兼容 我的实现如下所示: public function offsetSet($offset, $value) { if (!is_string($offset)) { throw new \LogicException("..."); } $this->params[$offset] = $value; } c

我现在正在从事一个项目,我有一个类实现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]);
  }
}
所以,在我看来,我的实现是正确的。你知道怎么了吗?非常感谢

这个类看起来像这样:

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

这个类看起来像这样:

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

这里唯一吸引我眼球的是:

 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);
  }
他会把把戏搞定的


它也可能是一个语法错误,从您尚未粘贴的代码部分拖拽而来。

在我看来就像是您的
名称空间
或文件顶部的
使用
指令,使其查找与之兼容的错误
ArrayAccess
接口。但如果没有这些指令,我无法确定

一般来说:

您自己的名称空间不应以反斜杠开头或结尾:

使用:
namespace-Web\Http

不要使用类似以下内容:
namespace\Web\Http
名称空间\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 */
}

哪一行引发错误?带有类声明的行(类X实现\ArrayAccess)。如果提供类的其余部分(或其缩短版本),可能会有所帮助。如果停止抛出异常,会发生什么情况?类声明是否在命名空间内?e、 g.您是否有
名称空间My\namespace文件顶部的声明?如果不是,则不需要限定ArrayAccess。您还可以尝试“使用”ing
ArrayAccess
,而不是完全限定它。这似乎是一个奇怪的错误,所以我只是在这里抛出一些东西…谢谢,但尽管这肯定是正确的,并且它提高了代码的可读性,但它并没有帮助我解决这个问题。这个语法错误是个好主意,但在删除实现后,类再次完美地工作。您的代码在这里工作得很好。将其粘贴到文件中,并使用:php test.php从命令行运行它。必须是代码中没有粘贴的部分。嘿,你使用的是什么版本的PHP?PHP5.3.X?我刚才也做了同样的事情,对实现进行了注释,结果成功了。上面的代码很好。反正应该没事。我并不是说Netbeans 6.8 IDE是完全可靠的,但它并没有报告任何语法错误。