Sar*_*aaz 1 php properties class object access-modifiers
所以我正在探索Reflection类的使用。我注意到一些事情。即使在 Origin 类中,也必须先设置属性的可访问性,然后才能使用属性的值或名称。
我想知道是否可以通过ReflectionClass
. 例如
class MyClass
{
public $bathroom = 'Dirty';
protected $individual = 'President';
private $conversation = '****************';
function outputReflectedPublic()
{
$reflection = new ReflectionClass($this);
$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($props as $prop)
echo $prop->getName() . ' : ' . $prop->getValue($this);
}
}
$obj = new MyClass();
$obj->outputReflectedPublic();//bathroom : Dirty
//now we add a new property
$obj->$ect = 'ify';
$obj->outputReflectedPublic();//bathroom : Dirty //same as before
Run Code Online (Sandbox Code Playgroud)
现在我对此并不感到太惊讶。我试图查看该属性是否作为受保护/私有/静态位于实例中ReflectionProperty::IS_PRIVATE
,并带有 ,ReflectionProperty::IS_PROTECTED
和ReflectionProperty::IS_STATIC
我还用来$prop->setAccessible(true)
防止无法访问的错误。我无法看到该$ect
房产。
我能够$ect
通过内部函数获取该属性,如下所示:
function getAll()
{
foreach($this as $key=>$val)
echo $key . ' : ' . $val . '<br>';
}
Run Code Online (Sandbox Code Playgroud)
浴室:脏
个人:主席
对话:****************
等:确定
有没有办法$ect
从 ReflectionClass 的对象中获取 that( ) 类型的属性?这些属性的正式名称是什么?
小智 5
ReflectionClass::getProperties() 仅获取由类显式定义的属性。要反映在对象上设置的所有属性(包括动态属性),请使用继承自 ReflectionClass 并适用于运行时实例的 ReflectionObject:
$reflect = new ReflectionObject($this);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
636 次 |
最近记录: |