PHP 5.3 and interface \ArrayAccess

Posted by Jakub Lédl on Stack Overflow See other posts from Stack Overflow or by Jakub Lédl
Published on 2010-02-13T13:25:04Z Indexed on 2010/06/14 12:22 UTC
Read the original article Hit count: 179

Filed under:

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

© Stack Overflow or respective owner

Related posts about php