PHP:空不适用于 getter 方法

发布于 2024-11-13 23:11:27 字数 879 浏览 4 评论 0原文

我有一个“getter”方法,就像

function getStuff($stuff){
  return 'something';
}

我用 empty($this->stuff) 检查它一样,我总是得到 FALSE,但我知道 $this ->stuff 返回数据,因为它与 echo 一起使用。

如果我用 !isset($this->stuff) 检查它,我会得到正确的值,并且条件永远不会执行...

这是测试代码:

class FooBase{

  public function __get($name){
    $getter = 'get'.ucfirst($name);
    if(method_exists($this, $getter)) return $this->$getter();
    throw new Exception("Property {$getter} is not defined.");
  }
}

class Foo extends FooBase{
  private $my_stuff;

  public function getStuff(){
    if(!$this->my_stuff) $this->my_stuff = 'whatever';
    return $this->my_stuff;
  }

}

$foo = new Foo();
echo $foo->stuff;

if(empty($foo->stuff)) echo 'but its not empty:(';
if($foo->stuff) echo 'see?';

I have a "getter" method like

function getStuff($stuff){
  return 'something';
}

if I check it with empty($this->stuff), I always get FALSE, but I know $this->stuff returns data, because it works with echo.

and if I check it with !isset($this->stuff) I get the correct value and the condition is never executed...

here's the test code:

class FooBase{

  public function __get($name){
    $getter = 'get'.ucfirst($name);
    if(method_exists($this, $getter)) return $this->$getter();
    throw new Exception("Property {$getter} is not defined.");
  }
}

class Foo extends FooBase{
  private $my_stuff;

  public function getStuff(){
    if(!$this->my_stuff) $this->my_stuff = 'whatever';
    return $this->my_stuff;
  }

}

$foo = new Foo();
echo $foo->stuff;

if(empty($foo->stuff)) echo 'but its not empty:(';
if($foo->stuff) echo 'see?';

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

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

发布评论

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

评论(3

韵柒 2024-11-20 23:11:27

empty() 会先调用 __isset(),只有返回 true 才会调用 __get()

实现 __isset() 并使其针对您支持的每个魔法属性返回 true

function __isset($name)
{
    $getter = 'get' . ucfirst($name);
    return method_exists($this, $getter);
}

empty() will call __isset() first, and only if it returns true will it call __get().

Implement __isset() and make it return true for every magic property that you support.

function __isset($name)
{
    $getter = 'get' . ucfirst($name);
    return method_exists($this, $getter);
}
半夏半凉 2024-11-20 23:11:27

当使用empty进行检查时不会调用Magic getter。该值确实不存在,因此 empty 返回 true。您还需要实现 __isset 才能使其正常工作。

__isset() 通过对不可访问的属性调用 isset()empty() 来触发。

http://www .php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

Magic getters are not called when checking with empty. The value really does not exist, so empty returns true. You will need to implement __isset as well to make that work correctly.

__isset() is triggered by calling isset() or empty() on inaccessible properties.

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

一枫情书 2024-11-20 23:11:27

PHP 的神奇的 get 方法是名为__get()$this->stuff 不会调用 getStuff()。试试这个:

public function __get($property) {
    if ($property == 'stuff') {
        return $this->getStuff();
    }
}

PHP's magic get method is named __get(). $this->stuff will not call getStuff(). Try this:

public function __get($property) {
    if ($property == 'stuff') {
        return $this->getStuff();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文