php - Return null by reference via __get() -


quick specs:

php 5.3 error_reporting(-1) // highest 

i'm using __get() reference trick magically access arbitrarily deep array elements in object.

quick example:

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

this doesn't work when $key isn't set, tries return null reference, of course throws only variable references should returned reference ... tried modifying follows:

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

still doesn't work though, i'm assuming setting $null null unset()s it.

what can do? thanks!


just figured i'd promote question, it's relevant (php magic , references); __callstatic(), call_user_func_array(), references, , php 5.3.1. i've yet find answer ...besides modifying php core.

this has nothing null, rather ternary operator:

rewriting if/else won't throw 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. can return values.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -