property_exists

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

property_exists檢查對象或類是否具有該屬性

說明

property_exists(mixed $class, string $property): bool

本函數檢查給出的 property 是否存在于指定的類中(以及是否能在當前范圍內訪問)。

注意:

As opposed with isset(), property_exists() returns true even if the property has the value null.

參數

class

字符串形式的類名或要檢查的類的一個對象

property

屬性的名字

返回值

如果該屬性存在則返回 true,如果不存在則返回 false,出錯返回 null

注釋

注意:

如果此類不是已知類,使用此函數會使用任何已注冊的 autoloader

注意:

The property_exists() function cannot detect properties that are magically accessible using the __get magic 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();

?>

參見