tags:

views:

196

answers:

4

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

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]);
  }
}
Jakub Lédl
You should probably delete this answer, btw.
Lior Cohen
How can I do this?
Jakub Lédl
Below the answer, there's an option "delete".
Lior Cohen
No there isn't (??).
Jakub Lédl
A: 

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.

Lior Cohen
Thanks, but although this is surely correct and it improves the code readability, it doesn't help me with this problem.
Jakub Lédl
Good idea with that syntax error, but after removing the implementation the class works perfectly again.
Jakub Lédl
Your code works fine here. Pasted it into a file and ran it from the command line using: php test.php. It has to be something in the part of the code you haven't pasted.
Lior Cohen
Hey, what version of PHP are you using? PHP 5.3.X?
Lior Cohen
I've just did the same, with the implementation commented out, and it worked. The code above is fine.It should be fine anyway. I don't say that Netbeans 6.8 IDE is completely reliable, but it doesn't report any syntax error.
Jakub Lédl
PHP version is 5.3.0.
Jakub Lédl
parenthesis here make no difference, everything between the `return` and the `?` is the condition, wether you put them in parenthesis or not. (it might add to the readability somewhat though)
Kris
A: 

What happens if you stop it throwing an exception?

Greg
Nothing. The error persists.
Jakub Lédl
A: 
Kris