ReflectionClass::getProperties

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getProperties获取一组属性

说明

public ReflectionClass::getProperties ( int $filter = ? ) : array

获取反射过的属性。

参数

filter

可选的过滤器,过滤为所需类型的属性。它使用 ReflectionProperty 常量 来配置,默认获取所有类型的属性。

返回值

ReflectionProperty 对象的数组。

范例

示例 #1 ReflectionClass::getProperties() 过滤例子

这个例子延时了可选 filter 参数的用法,例子里实际上忽略了私有属性。

<?php
class Foo {
    public    
$foo  1;
    protected 
$bar  2;
    private   
$baz  3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   $reflect->getProperties(ReflectionProperty::IS_PUBLIC ReflectionProperty::IS_PROTECTED);

foreach (
$props as $prop) {
    print 
$prop->getName() . "\n";
}

var_dump($props);

?>

以上例程的输出类似于:

foo
bar
array(2) {
  [0]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "foo"
    ["class"]=>
    string(3) "Foo"
  }
  [1]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "Foo"
  }
}

参见