&__get() 问题再次出现。重大挫折正在发生

发布于 2024-11-14 08:17:16 字数 1819 浏览 2 评论 0原文

好吧,我感到非常沮丧,即因为我认为我已经解决了这个问题,或者之前已经成功地完成了这个任务。

快速初步:

  • PHP 5.3.6。
  • 错误报告增加到 11。(-1 实际上;未来安全,所有错误/通知

我有一个类,它聚合请求参数。对于咯咯笑,这里有一个精简版本:

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!
    }
}

无论如何,arrg!的原因是,无论我尝试什么,每当$key时我总是会收到错误不存在。我已经尝试过:

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

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

我被告知三元运算符不能产生引用,因此,这当然不起作用,但我们从if 无论如何都要尝试条件。例如,会发生什么:

// 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'];

我在这里失去了理智;也许我幻想了一个实现这一目标的场景。是否不可能(不发出通知)成功地做到这一点?


澄清:我尝试过的事情

上述:

// 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;
}

其他尝试:

// 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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2024-11-21 08:17:16
 echo $myRequest->params['baz'];

__get 函数中的 isset 检查将从 $this->_data 查找 "params" 并返回数组。您收到的通知来自类外部,并且是关于返回数组中的键 "baz" - 在您的示例中从未实际定义过。

 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.

悲念泪 2024-11-21 08:17:16

我意识到这个问题已经过时了,但我只是在寻找答案时通过谷歌偶然发现了它(我后来找到了答案)。

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] 是一个返回值的操作,因此返回该值将导致错误,因为它不是引用。要使其返回引用,您必须使用引用:&$this->_data[$key]

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].

寻找我们的幸福 2024-11-21 08:17:16

没有尝试过这个,因为我避免使用 __get__set,但也许这对你有用:

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

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

完全未经测试,但看起来它可能可以完成这项工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文