property_exists
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
property_exists — 檢查對像或類是否具有該屬性
說明
本函式檢查給出的 property 是否存在於指定的類中(以及是否能在目前範圍內訪問)。
注意:
As opposed with isset(), property_exists() returns
trueeven if the property has the valuenull.
參數
-
class -
字串形式的類名或要檢查的類的一個對像
-
property -
屬性的名字
返回值
如果該屬性存在則返回 true,如果不存在則返回 false,出錯返回 null。
註釋
注意:
如果此類不是已知類,使用此函式會使用任何已註冊的 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();
?>