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

来源:互联网 发布:模拟电吉他软件下载 编辑:程序博客网 时间:2024/05/16 17:34

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官方文档。

0 0
原创粉丝点击