我有一个静态类Foo(这不是一个真正的类,所以静态字段只是例如)
class Foo{
public static $name = "foo";
public static $age = "18";
public static $city = "Boston";
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我想构建一个包含所有公共静态属性及其当前值的数组.
有没有一个快速/简单的方法,任何人都可以建议这样做而不实例化Foo?
Mat*_*son 12
使用这样的ReflectionClass
实例来获取属性名称和值的数组:
$class = new ReflectionClass('Foo');
$staticProperties = $class->getStaticProperties();
foreach ($staticProperties as $propertyName => $value) {
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
使用反射
<?php
require_once "Foo.php";
$reflection = new ReflectionClass("Foo");
$staticProperties = $reflection->getStaticProperties();
print_r($staticProperties)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2279 次 |
最近记录: |