0

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';
2
  • Because you return NULL unbound to a variable, does it help if you rewrite it to if(!isset( $this->data[ $offset ]) $this->data[ $offset ] = null; return $this->data[ $offset ]; ? /too lazy to test Commented Nov 24, 2014 at 19:18
  • Your function returns the result of an expression, not a reference. And a & null reference isn't possible. Try temporary variables. Commented Nov 24, 2014 at 19:18

3 Answers 3

1

In this code :

public function &offsetGet($offset) {
    $returnValue = isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
    return $returnValue;
}

$returnValue is a copy of $this->data[$offset], not a reference.

You have to make itself a reference, and for that you have to replace the ternary operator with an if statement :

public function &offsetGet($offset) {
    if (isset($this->data[$offset]) {
        $returnValue &= $this->data[$offset]; // note the &=
    }
    else {
        $returnValue = null;
    }
    return $returnValue;
}

should do the trick.

For the non-existent case, I'd rather throw an Exception, like the one you got when asking for a non-existing key of an array. Since the value you return won't be a reference,

$myclass['non-existing']['test2'] = array();

will presumably throw an indirect overloaded modification error, and should therefore be forbidden.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is becuase you are returning the result of an expression, not a variable. Try writing the if statement out and return the actual variable.

see php manual -> second note

Comments

0

The method '&offsetGet' is returning a reference (pointer) to a variable.

You need to either modify the method signature from '&offsetGet' to 'offsetGet' or use a variable to hold the return value.

// modify method signiture
public function offsetGet($offset) {
    return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
}

// or use a variable to hold the return value.
public function &offsetGet($offset) {
    $returnValue = isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
    return $returnValue;
}

3 Comments

I can't use the first statement because it's returning a copy of the array instead of the original. The second statement is throwing a new error: Indirect modification of overloaded element of My_Class has no effect
Give me the context up the stack. Whats calling offsetGet?
It's in the question. I'm creating a new class instance and then trying to set some data in the array

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.