我不能简单地使用get_class_vars()
,因为我需要它能够在PHP版本早于5.0.3的情况下工作(参见http://pl.php.net/get_class_vars Changelog)
另外:我该如何检查属性是否为公共属性?
我不能简单地使用get_class_vars()
,因为我需要它能够在PHP版本早于5.0.3的情况下工作(参见http://pl.php.net/get_class_vars Changelog)
另外:我该如何检查属性是否为公共属性?
<?php
class Foo {
public $alpha = 1;
protected $beta = 2;
private $gamma = 3;
}
$ref = new ReflectionClass('Foo');
print_r($ref->getProperties(ReflectionProperty::IS_PUBLIC));
结果是:
Array
(
[0] => ReflectionProperty Object
(
[name] => alpha
[class] => Foo
)
)
$getPublicProperties = create_function('$object', 'return get_object_vars($object);');
var_dump($getPublicProperties($this));
class Test implements IteratorAggregate
{
public PublicVar01 = "Value01";
public PublicVar02 = "Value02";
protected ProtectedVar;
private PrivateVar;
public function getIterator()
{
return new ArrayIterator($this);
}
}
$t = new Test()
foreach ($t as $key => $value)
{
echo $key." = ".$value."<br>";
}
PublicVar01 = Value01
PublicVar02 = Value02
\x00
),可以使用正则表达式排除它们。 - MrWhite