Php:如何通过反射列出静态字段/属性?
假设我有一门课:Php:如何通过反射列出静态字段/属性?,php,reflection,static-members,Php,Reflection,Static Members,假设我有一门课: class Example { public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2); public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223); } 如何使用反射获取静态字段列表?(类似于数组('$FOO','$BAR')?)您需要使用[Re
class Example {
public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2);
public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223);
}
如何使用反射获取静态字段列表?(类似于数组('$FOO','$BAR')?)您需要使用[
ReflectionClass][1]
。该函数将返回一个对象数组。ReflectionProperty
对象有一个方法可以告诉您该属性是否是静态的,还有一个方法可以返回名称
示例:
<?php
class Example {
public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2);
public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223);
}
$reflection = new ReflectionClass('Example');
$properties = $reflection->getProperties();
$static = array();
if ( ! empty($properties) )
foreach ( $properties as $property )
if ( $property->isStatic() )
$static[] = $property->getName();
print_r($static);
您需要使用[ReflectionClass][1]
。该函数将返回一个对象数组。ReflectionProperty
对象有一个方法可以告诉您该属性是否是静态的,还有一个方法可以返回名称
示例:
<?php
class Example {
public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2);
public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223);
}
$reflection = new ReflectionClass('Example');
$properties = $reflection->getProperties();
$static = array();
if ( ! empty($properties) )
foreach ( $properties as $property )
if ( $property->isStatic() )
$static[] = $property->getName();
print_r($static);