您的位置:首页 > 运维架构

property_exists 检查对象或类是否具有该属性

2016-05-04 16:48 453 查看
bool property_exists ( mixed
$class
, string
$property
)

1.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, as of PHP 5.3.0

var_dump(property_exists('myClass', 'bar')); //false

var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0

myClass::test();

2.class TestClass {

public $declared = null;

}

$testObject = new TestClass;

var_dump(property_exists("TestClass", "dynamic")); // boolean false, as expected

var_dump(property_exists($testObject, "dynamic")); // boolean false, same as above

$testObject->dynamic = null;

var_dump(property_exists($testObject, "dynamic")); // boolean true

unset($testObject->dynamic);

var_dump(property_exists($testObject, "dynamic")); // boolean false, again.

var_dump(property_exists($testObject, "declared")); // boolean true, as espected

unset($testObject->declared);

var_dump(property_exists($testObject, "declared")); // boolean true, even if has been unset()

其他例子参考php官方文档。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: