6

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.

Community
  • 1
  • 1
Dan Lugg
  • 18,516
  • 18
  • 99
  • 168

3 Answers3

19

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.

webbiedave
  • 46,141
  • 7
  • 83
  • 96
4

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.

Matthieu Napoli
  • 42,736
  • 37
  • 154
  • 239
  • Thanks **Matthieu**; I considered that, however I think returning `null` will be my best bet under the circumstances. – Dan Lugg May 11 '11 at 15:47
4

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];
}
Jason McCreary
  • 66,624
  • 20
  • 123
  • 167
  • By golly! The simplest answer obscured by overusing `isset` as per "best practices". Thanks **Jason McCreary**! – Dan Lugg May 11 '11 at 15:49
  • I'm not advocating the disuse of `isset()`. But considering your logic and the dynamic nature of PHP, this accomplishes the same thing. It may report a `Notice` though for keys that do not exist. You should check out **webbiedave's** answer if this bothers you. – Jason McCreary May 11 '11 at 15:52
  • I didn't receive any notices though. Is `error_reporting(-1)` excluding something? I read it covers all reporting levels, even future safe. – Dan Lugg May 11 '11 at 15:55
  • It may not due to the pass by reference. However, you should pass `error_reporting()` the appropriate flags. Not the explicit value - http://www.php.net/manual/en/function.error-reporting.php – Jason McCreary May 11 '11 at 15:57
  • On the contrary; the tip provided on that page explains `-1` is a future safe full reporting level. I would think that would be best (*at least for cross-platform development environments*) – Dan Lugg May 11 '11 at 15:59
  • Fair enough. I didn't scroll down to see that as the parameter description didn't mention `-1`. Good to know. Thanks. – Jason McCreary May 11 '11 at 16:02
  • And thank you. I'm going with your answer for it's brevity, though SO should introduce two types of "correct answers"; one that answers the question explicitly (**webbiedave**'s) and one that provides an equally correct, optimized alternative (**yours**) – Dan Lugg May 11 '11 at 16:08
  • In the end it's what you decide is the answer for you. That's why there's the checkmark and upvotes. I gave **webbiedave** credit with a +1. – Jason McCreary May 11 '11 at 16:10