使用PHP检查对象或类中是否存在属性
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)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短