我知道PHP没有一个纯对象变量,但我想检查属性是否在给定的对象或类中。

$ob = (object) array('a' => 1, 'b' => 12); 

or

$ob = new stdClass;
$ob->a = 1;
$ob->b = 2;

在JS中,我可以写这个来检查变量a是否存在于一个对象中:

if ('a' in ob)

在PHP中,可以做到这一点吗?


当前回答

isset和property_exists都不适合我。

如果属性存在但为NULL, isset返回false。 如果属性是对象类定义的一部分,Property_exists返回true,即使它没有被设置。

最后我的答案是:

    $exists = array_key_exists($property, get_object_vars($obj));

例子:

    class Foo {
        public $bar;

        function __construct() {
            $property = 'bar';

            isset($this->$property); // FALSE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this)); // TRUE

            unset($this->$property);

            isset($this->$property); // FALSE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this)); // FALSE

            $this->$property = 'baz';

            isset($this->$property); // TRUE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this));  // TRUE
        }
    }

其他回答

如果您想知道某个属性是否存在于已定义的类的实例中,只需将property_exists()与isset()结合起来即可。

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

isset和property_exists都不适合我。

如果属性存在但为NULL, isset返回false。 如果属性是对象类定义的一部分,Property_exists返回true,即使它没有被设置。

最后我的答案是:

    $exists = array_key_exists($property, get_object_vars($obj));

例子:

    class Foo {
        public $bar;

        function __construct() {
            $property = 'bar';

            isset($this->$property); // FALSE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this)); // TRUE

            unset($this->$property);

            isset($this->$property); // FALSE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this)); // FALSE

            $this->$property = 'baz';

            isset($this->$property); // TRUE
            property_exists($this, $property); // TRUE
            array_key_exists($property, get_object_vars($this));  // TRUE
        }
    }

这是非常新的,所以请确保您运行的是PHP 8:

$ob?->a

参考链接

要检查属性是否存在,以及是否也为空,可以使用函数property_exists()。

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

与isset()相反,即使属性值为NULL, property_exists()也会返回TRUE。

Bool类型:property_exists(混合$class,字符串$property)

例子:

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

我只是说说我的看法。

给定以下类:

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);
  }
}

将会发生以下情况:

$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

希望这对大家有所帮助