(PHP 5 >= 5.1.0, PHP 7, PHP 8)
property_exists — 檢查對象或類是否具有該屬性
本函數(shù)檢查給出的 property 是否存在于指定的類中(以及是否能在當前范圍內(nèi)訪問)。
注意:
As opposed with isset(), property_exists() returns
trueeven if the property has the valuenull.
class字符串形式的類名或要檢查的類的一個對象
property屬性的名字
如果該屬性存在則返回 true,如果不存在則返回 false,出錯返回 null。
注意:
如果此類不是已知類,使用此函數(shù)會使用任何已注冊的 autoloader。
注意:
The property_exists() function cannot detect properties that are magically accessible using the
__getmagic method.
| 版本 | 說明 |
|---|---|
| 5.3.0 | This function checks the existence of a property independent of accessibility. |
示例 #1 A property_exists() example
<?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, 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();
?>