简体   繁体   中英

PHP accessing private members of an object

I have seen this question once before but I am looking for a little more clarification related to my situation. What I am trying to do is access event data from the google calendar api using php. The data is returned in an object but the information I need to parse and use is protected. I am not sure how to get around this. This thread

How to get protected property value of object in PHP

is essentially identical to my current situation and it seems like the person found a solution yet didn't post any code or any sort of update to reflect what was done. Hopefully some one can clarify it more. Here is the code I am currently using, insight would be greatly appreciated

<?php
require_once 'vendor/autoload.php';

$maxEvents = 100;
$minStartDate = date('c');
$maxEndDate = date('c',strtotime("+1 day"));

$calendarId = '<MY CALENDAR ID>';

putenv("GOOGLE_APPLICATION_CREDENTIALS=<MY CREDENTIALS>");
$scope = 'https://www.googleapis.com/auth/calendar';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);

$options = array(
    'maxResults'    => $maxEvents,
    'orderBy'       => 'startTime',
    'singleEvents'  => TRUE,
    'timeMin'       => $minStartDate,
    'timeMax'       => $maxEndDate,
);

$results = $service->events->listEvents($calendarId, $options);
$events = $results->getItems();

$num = count($events);
echo $num . '<br/>';
echo 'results<br><pre>';print_r($events); echo '</pre><br>';

echo $events[1]->summary;
print_r($events[1]->modelData['creator']->email);

The print_r statements show the following (I'm truncating it)

Array
(
    [0] => Google_Service_Calendar_Event Object
    (
        [collection_key:protected] => recurrence
        [anyoneCanAddSelf] => 
        [attachmentsType:protected] => Google_Service_Calendar_EventAttachment
        [attachmentsDataType:protected] => array
        [attendeesType:protected] => Google_Service_Calendar_EventAttendee
        [attendeesDataType:protected] => array
        [attendeesOmitted] => 
        [colorId] => 
        [conferenceDataType:protected] => Google_Service_Calendar_ConferenceData
        [conferenceDataDataType:protected] => 
        [created] => 2017-11-02T18:29:30.000Z
        [creatorType:protected] => Google_Service_Calendar_EventCreator
        [creatorDataType:protected] => 
        [description] => 
Original Schedule Date - 2018-12-12
        [endType:protected] => Google_Service_Calendar_EventDateTime
        [endDataType:protected] => 
        [endTimeUnspecified] => 
        [etag] => "3034871549678000"
        [extendedPropertiesType:protected] => Google_Service_Calendar_EventExtendedProperties
        [extendedPropertiesDataType:protected] => 
        [gadgetType:protected] => Google_Service_Calendar_EventGadget
        [gadgetDataType:protected] => 
        [guestsCanInviteOthers] => 
        [guestsCanModify] => 
        [guestsCanSeeOtherGuests] => 
        [hangoutLink] => 
        [htmlLink] => 
        [iCalUID] => 
        [id] => 
        [kind] => calendar#event
        [location] => 
        [locked] => 
        [organizerType:protected] => Google_Service_Calendar_EventOrganizer
        [organizerDataType:protected] => 
        [originalStartTimeType:protected] => Google_Service_Calendar_EventDateTime
        [originalStartTimeDataType:protected] => 
        [privateCopy] => 
        [recurrence] => 
        [recurringEventId] => 
        [remindersType:protected] => Google_Service_Calendar_EventReminders
        [remindersDataType:protected] => 
        [sequence] => 2
        [sourceType:protected] => Google_Service_Calendar_EventSource
        [sourceDataType:protected] => 
        [startType:protected] => Google_Service_Calendar_EventDateTime
        [startDataType:protected] => 
        [status] => confirmed
        [summary] => 
        [transparency] => 
        [updated] => 2018-01-31T21:56:14.839Z
        [visibility] => 
        [internal_gapi_mappings:protected] => Array
            (
            )

        [modelData:protected] => Array
            (
                [creator] => Array
                    (
                        [email] => 
                        [displayName] => 
                    )

                [organizer] => Array
                    (
                        [email] => 
                        [self] => 1
                    )

                [start] => Array
                    (
                        [date] => 2018-07-25
                    )

                [end] => Array
                    (
                        [date] => 2018-07-26
                    )

                [attendees] => Array
                    (
                        [0] => Array
                            (
                                [email] => 
                                [responseStatus] => needsAction
                            )

                    )

                [reminders] => Array
                    (
                        [useDefault] => 1
                    )

            )

        [processed:protected] => Array
            (
            )

    )

Like the post from the link I posted I too am looking to access the start and end dates of each event.

您可以使用get_object_vars() ,它返回一个包含对象非静态属性的关联数组。

print_r(get_object_vars($events[1])['modelData']['creator']->email);

Thanks to all your help, it pointed me in the right direction. I found the following solution

    $events = $service->events->listEvents($calendarId, $options);

    foreach ($events->getItems() as $event) {
        echo "Summary:  ", $event->getSummary(), "<br/>";
        echo "Location: ", $event->getLocation(), "<br/>";
        echo "Start:    ", parse_date($event->getStart()), "<br/>";
        echo "End:      ", parse_date($event->getEnd()), "<br/>";
        echo '<br/>';
    }

    function parse_date($date){
        $val = $date->getDate();
        return (new DateTime($val))->format('d/m/Y');
    }

This at least gives me the information I need which will be used in other functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM