How to implement __isset() magic method in PHP?

后端 未结 3 442
名媛妹妹
名媛妹妹 2020-12-18 23:18

I\'m trying to make functions like empty() and isset() work with data returned by methods.

What I have so far:

abstract cla         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 00:00

    A bit more option not to depend on getter

    public function __isset($name)
    {
        $getter = 'get' . ucfirst($name);
        if (method_exists($this, $getter)) {
            return !is_null($this->$getter());
        } else {
            return isset($this->$name);
        }
    }
    

提交回复
热议问题