PHP:empty不适用于getter方法

时间:2021-07-10 13:20:27

I have a "getter" method like

我有一个像“getter”这样的方法

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.

如果我用空($ this-> stuff)检查它,我总是得到FALSE,但我知道$ this-> stuff返回数据,因为它适用于echo。

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

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

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?';

3 个解决方案

#1


22  

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

empty()将首先调用__isset(),并且只有返回true才会调用__get()。

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

实现__isset()并使其为您支持的每个魔术属性返回true。

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

#2


4  

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.

使用空检查时不会调用魔术吸气剂。该值确实不存在,因此empty返回true。您还需要实现__isset才能使其正常工作。

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

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

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

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

#3


1  

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

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

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

#1


22  

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

empty()将首先调用__isset(),并且只有返回true才会调用__get()。

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

实现__isset()并使其为您支持的每个魔术属性返回true。

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

#2


4  

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.

使用空检查时不会调用魔术吸气剂。该值确实不存在,因此empty返回true。您还需要实现__isset才能使其正常工作。

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

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

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

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

#3


1  

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

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

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