PHP check whether property exists in object or class

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

    property_exists( mixed $class , string $property )

    if (property_exists($ob, 'a')) 
    

    isset( mixed $var [, mixed $... ] )

    if (isset($ob->a))
    

    isset() will return false if property is null

    Example 1:

    $ob->a = null
    var_dump(isset($ob->a)); // false
    

    Example 2:

    class Foo
    {
       public $bar = null;
    }
    
    $foo = new Foo();
    
    var_dump(property_exists($foo, 'bar')); // true
    var_dump(isset($foo->bar)); // false
    
    0 讨论(0)
  • 2020-12-02 11:23

    To check if the property exists and if it's null too, you can use the function property_exists().

    Docs: http://php.net/manual/en/function.property-exists.php

    As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

    bool property_exists ( mixed $class , string $property )

    Example:

    if (property_exists($testObject, $property)) {
        //do something
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-02 11:26

    Just putting my 2 cents here.

    Given the following class:

    class Foo
    {
      private $data;
    
      public function __construct(array $data)
      {
        $this->data = $data;
      }
    
      public function __get($name)
      {
        return $data[$name];
      }
    
      public function __isset($name)
      {
        return array_key_exists($name, $this->data);
      }
    }
    

    the following will happen:

    $foo = new Foo(['key' => 'value', 'bar' => null]);
    
    var_dump(property_exists($foo, 'key'));  // false
    var_dump(isset($foo->key));  // true
    var_dump(property_exists($foo, 'bar'));  // false
    var_dump(isset($foo->bar));  // true, although $data['bar'] == null
    

    Hope this will help anyone

    0 讨论(0)
  • 2020-12-02 11:27

    Using array_key_exists() on objects is Deprecated in php 7.4

    Instead either isset() or property_exists() should be used

    reference : php.net

    0 讨论(0)
提交回复
热议问题