如何获得类的公共属性?

Kam*_*zot 5 php properties class public

我不能简单地使用get_class_vars()它因为我需要它使用早于5.0.3的PHP版本(参见http://pl.php.net/get_class_vars Changelog)

或者:我如何检查财产是否公开?

Kam*_*zot 7

这可以通过使用反射来实现.

<?php

class Foo {
  public $alpha = 1;
  protected $beta = 2;
  private $gamma = 3;
}

$ref = new ReflectionClass('Foo');
print_r($ref->getProperties(ReflectionProperty::IS_PUBLIC));
Run Code Online (Sandbox Code Playgroud)

结果是:

Array
(
    [0] => ReflectionProperty Object
        (
            [name] => alpha
            [class] => Foo
        )

)
Run Code Online (Sandbox Code Playgroud)