PHP: empty doesn't work with a getter method

前端 未结 3 620
北恋
北恋 2020-12-18 22:12

I have a \"getter\" method like

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

if I check it with empty($this->stuff)

3条回答
  •  感动是毒
    2020-12-18 22:28

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

提交回复
热议问题