(PHP 5 >= 5.1.0, PHP 7, PHP 8)
property_exists — 检查对象或类是否具有该属性
$object_or_class, string $property): bool
本函数检查给出的 property 是否存在于指定的类中。
注意:
跟 isset() 的区别是即使属性的值为
null,property_exists() 也会返回true。
object_or_class
需要检查的类名或者类的对象
property
属性的名称
如果属性存在则返回 true,不存在则返回 false。如果发生错误则返回 null。
示例 #1 property_exists() 示例
<?php
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
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //true
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); //true
myClass::test();
?>