PHP 中的 property_exists() 函数
phpprogrammingserver side programming更新于 2025/8/29 22:22:17
property_exists() 方法检查对象或类是否具有属性。
语法
property_exists(object, property)
参数
object/ class − 对象或类的名称
property − 属性的名称
返回
如果属性存在,property_exists() 函数返回 TRUE;如果不存在,返回 FALSE;如果发生错误,返回 NULL。
示例
下面是一个例子 −
<?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)

