开发者

PHP 5.3 and interface \ArrayAccess

开发者 https://www.devze.com 2022-12-20 05:59 出处:网络
I\'m now working on a project and I have one class that implements the ArrayAccess interface. Howewer, I\'m getting an error that says that my implementation:

I'm now working on a project and I have one class that implements the ArrayAccess interface.

Howewer, I'm getting an error that says that my implementation:

must be compatible with that of ArrayAccess::offsetSet().

My implementation looks like this:

public function offsetSet($offset, $value) {
  if (!is_string($offset)) {
    throw new \LogicException("...");
  }
  $this->params[$offset] = $value;
}

So, to me it looks like my implementation is correct. Any idea what is wrong? Thanks very much!

The class look like this:

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

The class look like this:

class HttpRequest implements \Arra开发者_Python百科yAccess {
  // 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]);
  }
}


Looks to me like your namespace or use directives in the top of the file make it look for the wrong ArrayAccess interface to be compatible with. Can't tell for sure without those directives though.

In general:

Your own namespaces should not begin or end with a backslash:

Use: namespace Web\Http;

Don't use something like: namespace \Web\Http; or namespace \Web\Http\;

For every class and interface you reference in your file, add a use directive:

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 */
}


The only thing that catches my eye here:

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

Perhaps replacing it with:

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

would get the trick done.

It could also be a syntax error that drags on from the part of the code you haven't pasted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号