property_exists

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

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

说明

property_exists(object|string $object_or_class, string $property): bool

本函数检查给出的 property 是否存在于指定的类中。

注意:

isset() 的区别是即使属性的值为 nullproperty_exists() 也会返回 true

参数

object_or_class

需要检查的类名或者类的对象

property

属性的名称

返回值

如果属性存在则返回 true,不存在则返回 false。如果发生错误则返回 null

范例

示例 #1 property_exists() 示例

<?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
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); //true
myClass::test();

?>

注释

注意:

如果此类不是已知类,使用此函数会使用任何已注册的 autoloader

注意:

property_exists() 函数不能检查通过 __get 魔术方法访问的属性。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top