PHP check whether property exists in object or class

后端 未结 8 1531
情深已故
情深已故 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:23

    To check if something exits, you can use the PHP function isset() see php.net. This function will check if the variable is set and is not NULL.

    Example:

    if(isset($obj->a))
    { 
      //do something
    }
    

    If you need to check if a property exists in a class, then you can use the build in function property_exists()

    Example:

    if (property_exists('class', $property)) {
        //do something
    }
    

提交回复
热议问题