I read a lot of past questions about ArrayAccess PHP interface and it's method offsetGet that can return a reference. I have a simple class implementing this interface that wraps a variable of type array. The offsetGet method returns a reference, however I get an error saying Only variable references should be returned by reference. Why?
class My_Class implements ArrayAccess {
private $data = array();
...
public function &offsetGet($offset) {
return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
}
...
}
I would like to be able to use multidimensional arrays with this class:
$myclass = new My_Class();
$myclass['test'] = array();
$myclass['test']['test2'] = array();
$myclass['test']['test2'][] = 'my string';
NULLunbound to a variable, does it help if you rewrite it toif(!isset( $this->data[ $offset ]) $this->data[ $offset ] = null; return $this->data[ $offset ];? /too lazy to test& nullreference isn't possible. Try temporary variables.