首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ReflectionClass::getProperties

(PHP 5, PHP 7)

ReflectionClass::getProperties - 获取属性

描述

代码语言:javascript
复制
public array ReflectionClass::getProperties ([ int $filter ] )

检索反射的属性。

参数

filter

可选的过滤器,用于过滤所需的属性类型。它使用 ReflectionProperty 常量进行配置,并且默认为所有属性类型。

返回值

ReflectionProperty 对象的数组。

例子

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

这个例子演示了可选filter参数的用法,它实际上跳过了私有属性。

代码语言:javascript
复制
<?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);

?>

上面的例子会输出类似于:

代码语言:javascript
复制
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"
  }
}

另请参阅

  • ReflectionClass::getProperty() - 获取类的属性的ReflectionProperty
  • ReflectionProperty
  • ReflectionProperty修饰符常量

← ReflectionClass::getParentClass

ReflectionClass::getProperty →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com