Search code examples
phpstdclass

Accessing StdClass value with colon :protected


How do I access the value of stdClass with colon ":protected"?

For example, I had this $obj with these result :

object(Google_Service_Plus_PeopleFeed)#14 (11) {
  ["title"]=>
  string(30) "Google+ List of Visible People"
  ["totalItems"]=>
  int(4)
  ["collection_key:protected"]=>
  string(5) "items"
  ["data:protected"]=>
  array(1) {
    ["items"]=>
    array(2) {
      [0]=>
      array(7) {
        ["kind"]=>
        string(11) "plus#person"
        ["etag"]=>
        string(57) ""42gOj_aEQqJGtTB3WnOUT5yUTkI/1eNkvlfeTwXXldr9rYAvMcwM6bk""
        ["objectType"]=>
        string(6) "person"

For example, I tried to access "kind" value which is "plus#person" using these code :

$kind = $obj->{'data:protected'}->items[0]->kind; //-> returns NULL
//OR
$kind = $obj->{data:protected}->items[0]->kind; //->returns error on ":"

Well, they don't seems to work...Any idea how to access that protected data?

Thanks


Solution

  • It's not a stdClass object, it's an object of the class Google_Service_Plus_PeopleFeed. You cannot access protected properties of a class [easily]. If the class doesn't want you to access the data, then you shouldn't. But typically the class offers some method you can call to get the data, like $obj->getData() or some such. Look at the class definition or its documentation to see how you're supposed to use the class.