定义和用法
property_exists - 检查对象或类是否具有该属性
版本支持
PHP4 | PHP5 | PHP7 |
---|---|---|
不支持 | V5.1.0(含)+支持 | 支持 |
5.3.0 此函数检查是否存在与可访问性无关的属性。
语法
property_exists ( mixed $class , string $property )
本函数检查给出的 property 是否存在于指定的类中(以及是否能在当前范围内访问)。
与 isset()相反,即使属性值为NULL, property_exists()也返回TRUE。
参数
参数 | 必需的 | 描述 |
---|---|---|
class | 是 | 字符串形式的类名或要检查的类的一个对象 |
property | 是 | 属性的名字 |
返回值
如果该属性存在则返回 TRUE,如果不存在则返回 FALSE,出错返回 NULL。
如果此类不是已知类,使用此函数会使用任何已注册的类。
示例
class myClass {
public $mine;
private $xpto;
static protected $test;
static function test() {
var_dump(property_exists('myClass', 'xpto')); //true
}
}
var_dump(property_exists('myClass', 'mine')); //true
echo '<br/>';
var_dump(property_exists(new myClass, 'mine')); //true
echo '<br/>';
var_dump(property_exists('myClass', 'xpto')); //true, as of PHP 5.3.0
echo '<br/>';
var_dump(property_exists('myClass', 'bar')); //false
echo '<br/>';
var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0
echo '<br/>';
myClass::test();
相关函数
method_exists() - 检查类的方法是否存在