property_exists()或isset()函数可用于检查属性是否存在于类或对象中。
以下是property_exists()函数的语法-
property_exists( mixed $class , string $property )例
if (property_exists($object, a_property))以下是isset()函数的语法-
isset( mixed $var [, mixed $... ] )例
if (isset($object->a_property))如果a_property为null,则isset()将返回false。
让我们看一个例子-
<?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)); ?>输出结果
bool(true) bool(true)