【发布时间】:2013-12-18 12:26:58
【问题描述】:
我有一个对象,它有一些我想要获取和设置的受保护属性。对象看起来像
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 或$obj->value 时,它会产生错误。我搜索并找到了使用PHP Reflection Class 的解决方案。它在我的本地工作,但在服务器 PHP 版本是 5.2.17 所以我不能在那里使用这个函数。那么任何解决方案如何获得这样的属性?
【问题讨论】:
-
使用getter和setter
-
您是否省略了一些上下文?您只需要编写适当的 setter/getter 方法对。如果你不能修改这个类,你可以简单地扩展它。
-
@Arnaud 我认为他已经了解 OOP 的基础知识。我认为这里真正的问题是他不能修改 Fields_Form_Element_Location 类。
-
查看类代码或文档是否为您提供任何 getter 来访问这些数据。如果没有,您不应该访问它。找出为什么。如果您仍然需要访问它,则需要修改该类和/或与其作者交谈。
-
“付费工具”没有文档?那你付什么钱? D-;
标签: php object protected php-5.2