Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return null by reference via __get()

Quick specs:

PHP 5.3
error_reporting(-1) // the highest

I'm using the __get() by reference trick to magically access arbitrarily deep array elements in an object.

Quick example:

public function &__get($key){
    return isset($this->_data[$key])
        ? $this->_data[$key]
        : null;
}

This doesn't work as when the $key isn't set, it tries to return null by reference, which of course throws Only variable references should be returned by reference ... I tried modifying it as follows:

public function &__get($key){
    $null = null;
    return isset($this->_data[$key])
        ? $this->_data[$key]
        : $null;
}

Still doesn't work though, I'm assuming that setting $null to null essentially unset()s it.

What can I do? Thanks!


Just figured I'd promote this question, as it's somewhat relevant (PHP magic and references); __callStatic(), call_user_func_array(), references, and PHP 5.3.1. I've yet to find an answer ...besides modifying the PHP core.

like image 661
Dan Lugg Avatar asked May 11 '11 15:05

Dan Lugg


3 Answers

This has nothing to do with null, but rather the ternary operator:

Rewriting it with an if/else won't throw the notice:

public function &__get($key)
{
    $null = null;
    if (isset($this->_data[$key])) {
        return $this->_data[$key];
    } else {
        return $null;
    }
}

Ternary operators cannot result in references. They can only return values.

like image 79
webbiedave Avatar answered Nov 20 '22 03:11

webbiedave


I had this problem, but I ended up realizing that I shouldn't return null when the key wasn't found, but throw an exception (because I was accessing an unknown attribute after all).

But maybe that's not what you want to do, I just wanted to share that.

like image 45
Matthieu Napoli Avatar answered Nov 20 '22 05:11

Matthieu Napoli


Why return null explicitly? If $key doesn't exist in $this->_data it's going to return NULL anyway?

I recommend using the following and adjust your logic on the other end. You're probably already checking for null now. You could change it to empty() or some other variant. Or use exceptions as suggested by Matthieu.

public function &__get($key){
    return $this->_data[$key];
}
like image 4
Jason McCreary Avatar answered Nov 20 '22 03:11

Jason McCreary