如何在PHP中获取对象的受保护属性值 [英] How to get protected property value of object in PHP
问题描述
我有一个具有受保护属性的对象,我想获取 dateTime
.
I have a object with protected property that I want to get dateTime
.
1)modelData->start->dateTime //2017-05-10T17:00:00+05:30
2)modelData->end->dateTime //2017-05-10T17:20:00+05:30
物体看起来像
Google_Service_Calendar_Event Object
(
[collection_key:protected] => recurrence
[attachmentsType:protected] => Google_Service_Calendar_EventAttachment
[created] => 2017-05-08T07:05:43.000Z
[creatorType:protected] => Google_Service_Calendar_EventCreator
[creatorDataType:protected] =>
[description] =>
[endType:protected] => Google_Service_Calendar_EventDateTime
[endDataType:protected] =>
[endTimeUnspecified] =>
[etag] => "2988454353000000"
[extendedPropertiesType:protected] => Google_Service_Calendar_EventExtendedProperties
[extendedPropertiesDataType:protected] =>
[gadgetType:protected] => Google_Service_Calendar_EventGadget
[visibility] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
[creator] => Array
(
[email] => pickzy.nettest@gmail.com
[self] => 1
)
[organizer] => Array
(
[email] => pickzy.nettest@gmail.com
[self] => 1
)
[start] => Array
(
[dateTime] => 2017-05-10T17:00:00+05:30
[timeZone] => Asia/Calcutta
)
[end] => Array
(
[dateTime] => 2017-05-10T17:20:00+05:30
[timeZone] => Asia/Calcutta
)
[reminders] => Array
(
[useDefault] => 1
)
)
)
PHP 脚本:
我可以在不生产的情况下获得 etag 值
I can get etag value without producted
echo "-----".$result->getEtag(); //"2988454353000000"
我无法获得产品的开始和结束值
I cannot get start and end value with producted
echo "-----".$result = $event->getData(); //Error undefined method
我的推荐人:
请指教!
推荐答案
声明为protected的成员只能在类内访问本身和通过继承类.
Members declared protected can be accessed only within the class itself and by inheriting classes.
如果您需要从外部访问该属性,请选择一个:•不要将其声明为受保护的,而是将其公开• 编写几个函数来获取和设置值(getter 和 setter)
If you need to access the property from outside, pick one: •Don't declare it as protected, make it public instead •Write a couple of functions to get and set the value (getters and setters)
如果不想修改原来的类(因为它是一个第三方库,你不想弄乱)创建一个自定义类扩展原来的:
If you don't want to modify the original class (because it's a third-party library you don't want to mess) create a custom class that extends the original one:
class My_Google_Service_Calender_Event extends Google_Service_Calendar_Event {
}
这篇关于如何在PHP中获取对象的受保护属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!