简体   繁体   English

获取除静态属性外的所有属性

[英]Get all properties except static ones

I tried: 我试过了:

$rc = new \ReflectionClass($this);
$rc->getProperties(ReflectionProperty::IS_PUBLIC || ReflectionProperty::IS_PRIVATE || ReflectionProperty::IS_PROTECTED)

But all that did was give me the one static property that the class has. 但是所做的只是给我一个类拥有的static属性。 It has 3 private and one static . 它有3 private和1个static I just want to know how to weed out the static ones. 我只想知道如何清除static字段。

Your static properties are also public , private or protected , so it returns them as well. 您的static属性也是publicprivateprotected ,因此它也返回它们。 You need to check each property with isStatic() : 您需要使用isStatic()检查每个属性:

foreach($rc->getProperties() as $prop) {
    if(!$prop->isStatic()) {  // or $prop->isStatic() === false
        $result[] = $prop;
    }
}

getProperties() defaults to all properties. getProperties()默认为所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM