Unlike JavaScript, PHP does not inherently possess pure object variables. However, ascertaining whether a property exists within an object or class is possible using various approaches.
The property_exists() function allows for explicit checks on property existence. Its syntax is:
if (property_exists($ob, 'a'))
where $ob is the object or class instance.
Alternatively, isset() can verify if a property is set within an object. However, it's crucial to note that isset() returns false if the property's value is null.
if (isset($ob->a))
Here's an example demonstrating the differences:
$ob->a = null;
var_dump(isset($ob->a)); // false
Even though the property exists, isset() returns false due to the null value.
class Foo
{
public $bar = null;
}
$foo = new Foo();
var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false
In this scenario, property_exists() returns true since the property is defined, while isset() returns false because the value is null.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3