Determining if object property is empty

后端 未结 3 1406
清酒与你
清酒与你 2021-02-19 07:07

I feel like I\'m missing something here. I\'ve been using PHP\'s empty() function for quite a while now in determining if a variable is empty. I wanted to use it to

相关标签:
3条回答
  • 2021-02-19 07:18

    Check if the return value is null instead. Should give you the right answer.

    0 讨论(0)
  • 2021-02-19 07:23
    if (empty(($person->number)))
    
    /* OR */
    
    if (!isset($person->nothing) || empty(($person->nothing)))
    

    Putting () around the Object->Property value will force it to be evaluated prior to calling empty.

    0 讨论(0)
  • 2021-02-19 07:30

    You need to implement __isset() magic method.

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

    public function __isset($property){
        return isset($this->$property);
    } 
    
    0 讨论(0)
提交回复
热议问题