&__get() issues, again. Major frustration is afoot

末鹿安然 提交于 2019-12-11 18:19:19

问题


Alrighty, I'm getting quite frustrated, namely because I thought I had this issue solved, or had accomplished this successfully before.

Quick preliminary:

  • PHP 5.3.6.
  • Error reporting cranked to 11. (-1 actually; future safe, all errors/notices)

I have a class, it aggregates request parameters. For giggles here is a stripped down version:

class My_Request{
    private $_data = array();
    public function __construct(Array $params, Array $session){
        $this->_data['params']  = $params;
        $this->_data['session'] = $session;
    }
    public function &__get($key){
        // arrg!
    }
}

Anyways, the reason for arrg! is, no matter what I try, I always get an error whenever the $key doesn't exist. I've tried:

// doesn't work
$null = null;
if(isset($this->_data[$key])){ return $this->_data[$key]; }
return $null;

// doesn't work
return $this->_data[$key];

I've been told ternary operators cannot result in a reference, ergo, that of course doesn't work, but we know that from the if condition attempt anyways. What happens, for example:

// params will have foo => bar, and session hello => world
$myRequest = new My_Request(array('foo' => 'bar'), array('hello' => 'world'));

// throws an error - Undefined index: baz
echo $myRequest->params['baz'];

I'm losing my mind here; perhaps I hallucinated a scenario where I achieved this. Is it not possible to (without throwing a notice) successfully do this?


Clarification: Things I've tried

The aforementioned:

// no check, no anything, just try returning : fails
public function &__get($key){
    return $this->_data[$key];
}

// null variable to pass back by reference : fails
public function &__get($key){
    $null = null;
    if(isset($this->_data[$key])){
        return $this->_data[$key];
    }
    return $null;
}

Other attempts:

// can't work - can't return null by reference nor via ternary : fails
public function &__get($key){
    return isset($this->_data[$key])
        ? $this->_data[$key]
        : null;
}

回答1:


 echo $myRequest->params['baz'];

The isset check in your __get function will look up "params" from $this->_data and return the array. The notice you get is from outside the class and about a key "baz" in the returned array - which in your example was never actually defined.




回答2:


I realize this question is stale, but I just stumbled on it via Google while looking for the answer (which I have since found).

class My_Request{
    private $_data = array();
    public function __construct(Array $params, Array $session){
        $this->_data['params']  = $params;
        $this->_data['session'] = $session;
    }
    public function &__get($key){
        if (array_key_exists($key, $this->_data)) {
            return &$this->_data[$key]; // Note the reference operator
        } else {
            $value = null; // First assign null to a variable
            return $value; // Then return a reference to the variable
        }
    }
}

$this->_data[$key] is an operation that returns a value, so returning the value will result in an error because it's not a reference. To make it return a reference instead, you have to use the reference: &$this->_data[$key].




回答3:


Haven't tried this because I avoid __get and __set, but maybe this would work for you:

public function __get($key){
   if(!isset($this->_data[$key]))
       return false;

   return $this->_data[$key];    
}

Totally untested, but it looks like it could maybe do the job.



来源:https://stackoverflow.com/questions/6287982/get-issues-again-major-frustration-is-afoot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!