The property_exists() or the isset() function can be used to check if the property exists in the class or object.
Syntax
Below is the syntax of property_exists() function−
property_exists( mixed $class , string $property )
Example
if (property_exists($object, 'a_property'))
Below is the syntax of isset() function−
isset( mixed $var [, mixed $... ] )
Example
if (isset($object->a_property))
The isset() will return false if the ‘a_property’ is null.
Example
Let us see an example −
<?php class Demo { public $one; private $two; static protected $VAL; static function VAL() { var_dump(property_exists('myClass', 'two')); } } var_dump(property_exists('Demo', 'one')); var_dump(property_exists(new Demo, 'one')); ?>
Output
This will produce the following output−
bool(true) bool(true)