0

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 \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]);
  }
}
8
  • which line is throwing the error? Commented Feb 13, 2010 at 13:30
  • The line with class declaration (class X implements \ArrayAccess). Commented Feb 13, 2010 at 13:33
  • It could help if you provide the rest of the class (or a shortened version of it). Commented Feb 13, 2010 at 13:33
  • What happens if you stop it throwing an exception? Commented Feb 13, 2010 at 14:04
  • 1
    Is your class declaration inside a namespace? e.g. do you have a namespace My\Namespace; declaration at the top of the file? if not, you shouldn't need to qualify ArrayAccess. You could also try "use"ing ArrayAccess instead of fully qualifying it. This seems like a weird error, so I'm just throwing things out there... Commented Feb 14, 2010 at 3:00

2 Answers 2

1

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 */
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

Thanks, but although this is surely correct and it improves the code readability, it doesn't help me with this problem.
Good idea with that syntax error, but after removing the implementation the class works perfectly again.
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.
Hey, what version of PHP are you using? PHP 5.3.X?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.