ReflectionClass::getProperties
(PHP 5, PHP 7)
ReflectionClass::getProperties - 获取属性
描述
public array ReflectionClass::getProperties ([ int $filter ] )
检索反射的属性。
参数
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"
}
}
另请参阅
- ReflectionClass::getProperty() - 获取类的属性的ReflectionProperty
- ReflectionProperty
- ReflectionProperty修饰符常量
← ReflectionClass::getParentClass
ReflectionClass::getProperty →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com