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

发表日期:2021-07-01 08:57:12 | 来源: | | 浏览(770) 分类:类/对象 函数

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'));
   //truevar_dump(property_exists(new myClass, 'mine'));
 //truevar_dump(property_exists('myClass', 'xpto'));
   //true, as of PHP 5.3.0var_dump(property_exists('myClass', 'bar'));
    //falsevar_dump(property_exists('myClass', 'test'));
   //true, as of PHP 5.3.0myClass::test();
?>

参见

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。