我有一个对象具有一些我想要获取和设置的受保护属性。该物体看起来像
Fields_Form_Element_Location Object
(
[helper] => formText
[_allowEmpty:protected] => 1
[_autoInsertNotEmptyValidator:protected] => 1
[_belongsTo:protected] =>
[_description:protected] =>
[_disableLoadDefaultDecorators:protected] =>
[_errorMessages:protected] => Array
(
)
[_errors:protected] => Array
(
)
[_isErrorForced:protected] =>
[_label:protected] => Current City
[_value:protected] => 93399
[class] => field_container field_19 option_1 parent_1
)
我想要得到value
对象的属性。当我尝试时$obj->_value
or $obj->value
它会产生错误。我搜索并找到了可以使用的解决方案PHP Reflection Class
。它在我的本地工作,但在服务器上 PHP 版本是5.2.17
所以我不能在那里使用这个功能。那么有什么解决方案如何获得这样的财产呢?
这是如何使用的非常简单的示例(没有错误检查)ReflectionClass
:
function accessProtected($obj, $prop) {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($prop);
$property->setAccessible(true);
return $property->getValue($obj);
}
我知道你说过你的版本限制为 5.2,但那是 2 年前的事了,5.5 是最旧的受支持版本 http://php.net/supported-versions.php我希望能帮助人们使用现代版本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)