PHP检查对象或类中是否存在属性

时间:2021-04-16 23:46:00

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.

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

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

or

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

In JS, I can write this to check if variable a exists in an object:

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

if ('a' in ob)

In PHP, can anything like this be done?

在PHP中,可以做这样的事情吗?

Thank you very much for your advice.

非常感谢您的建议。

5 个解决方案

#1


84  

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

如果属性为空,isset()将返回false

Example 1:

示例1:

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

Example 2:

示例2:

class Foo
{
   public $bar = null;
}

$foo = new Foo();

var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false

#2


46  

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

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

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

文档: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.

与isset()相反,property_exists()返回TRUE,即使属性的值为空。

Example:

例子:

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

#3


5  

Neither isset or property_exists work for me.

isset或property_exist都不适合我。

  • isset returns false if the property exists but is NULL.
  • 如果属性存在但为空,则isset返回false。
  • property_exists returns true if the property is part of the object's class definition, even if it has been unset.
  • property_exists返回true,如果属性是对象的类定义的一部分,即使它是未设置的。

I ended up going with:

最后我说

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

Example:

例子:

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

#4


3  

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().

如果您想知道已定义的类的实例中是否存在属性,只需将property_exists()与isset()合并。

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

#5


0  

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.

要检查是否有东西退出,可以使用PHP函数isset()参见PHP .net。这个函数将检查变量是否设置为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()

如果需要检查类中是否存在属性,那么可以使用function property_exists()中的build

Example:

例子:

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

#1


84  

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

如果属性为空,isset()将返回false

Example 1:

示例1:

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

Example 2:

示例2:

class Foo
{
   public $bar = null;
}

$foo = new Foo();

var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false

#2


46  

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

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

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

文档: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.

与isset()相反,property_exists()返回TRUE,即使属性的值为空。

Example:

例子:

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

#3


5  

Neither isset or property_exists work for me.

isset或property_exist都不适合我。

  • isset returns false if the property exists but is NULL.
  • 如果属性存在但为空,则isset返回false。
  • property_exists returns true if the property is part of the object's class definition, even if it has been unset.
  • property_exists返回true,如果属性是对象的类定义的一部分,即使它是未设置的。

I ended up going with:

最后我说

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

Example:

例子:

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

#4


3  

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().

如果您想知道已定义的类的实例中是否存在属性,只需将property_exists()与isset()合并。

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

#5


0  

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.

要检查是否有东西退出,可以使用PHP函数isset()参见PHP .net。这个函数将检查变量是否设置为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()

如果需要检查类中是否存在属性,那么可以使用function property_exists()中的build

Example:

例子:

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