Need help with ArrayAccess conundrum
Posted: Thu May 14, 2009 3:37 pm
okay so I've got an object implementing ArrayAccess like so:
... and theeeeeen..... when I run the following code...
... i get the error:
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in foo.php.
Soooooooooo.... i tried changing the method definition of Foo::offsetGet() to return a reference like so...
.... buuuuuuuuuuuut... this conflicts with the definition of ArrayAccess::offsetGet, which does not return a reference, and i end up with this error:
Fatal error: Declaration of Foo::offsetGet() must be compatible with that of ArrayAccess::offsetGet() in foo.php
I can get both errors to go away by not attempting to assign the reference in the implementation like so...
I guess I don't know why PHP is throwing the first error. I don't understand why PHP is expecting a reference and a reference only. Any insight would be good. I've been surfing for this answer all morning.
Code: Select all
class Foo implements ArrayAccess{
private $data;
public function offsetGet($var) {
return $this->data[$var];
}
public function offsetSet($var, $value) {
$this->data[$var] = $value;
}
public function offsetExists($var) {
return isset($this->data[$var]);
}
public function offsetUnset($var) {
unset($this->data[$var]);
}
}
Code: Select all
$foo = new Foo();
$foo['stinky'] = 'weaselteats';
$ref =& $foo['stinky'];
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in foo.php.
Soooooooooo.... i tried changing the method definition of Foo::offsetGet() to return a reference like so...
Code: Select all
...
public function &offsetGet($var){
return $this->data[$var];
}
...
Fatal error: Declaration of Foo::offsetGet() must be compatible with that of ArrayAccess::offsetGet() in foo.php
I can get both errors to go away by not attempting to assign the reference in the implementation like so...
Code: Select all
$ref = $foo['stinky'];
// no errors - but this is not what I want