PHP check whether property exists in object or class

后端 未结 8 1530
情深已故
情深已故 2020-12-02 11:01

I understand PHP does not have a pure object variable, but I want to check whether a property is in the given object or class.

$ob = (object) array(\'a\' =&g         


        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 11:25

    If you want to know if a property exists in an instance of a class that you have defined, simply combine property_exists() with isset().

    public function hasProperty($property)
    {
        return property_exists($this, $property) && isset($this->$property);
    }
    

提交回复
热议问题