Access protected object in array in PHP

2019-09-07 15:44发布

问题:

I am trying to access the following and need to get the value of [vid] array cell.

FieldCollectionItemEntity Object
(
    [fieldInfo:protected] => 
    [hostEntity:protected] => stdClass Object
        (
            **[vid]** => 119
            [uid] => 1
            [title] => My Page Name
            [log] => 
            [status] => 1
            [comment] => 1
            [promote] => 0
            [sticky] => 0
            [vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
            [nid] => 119
            [type] => subpage
            [language] => und
            [created] => 1408621327
            [changed] => 1408640191
            [tnid] => 0
            [translate] => 0
            [uuid] => 39145013-6637-4062-96e7-1b4589609c4f
            [revision_timestamp] => 1408640191

I tried the following, but I guess I don't have a clue from here:-

  $mything = new myClass;
  print $mything->accessObjectArray();


  class myClass {
      protected $var;


      function accessObjectArray(){
        return $this-> $var;
      }
      //other member functions
  }

Update

I actually only have access to the variable $content which has the following multi-dimensional arrays. All I want is to get the array cell's value of [vid].

To do that, I could print $content["field_image_title"]["#object"] but after that it's protected. That's where I am wondering that how can I access this array. I unfortunately do not have access FieldCollectionItemEntity to include in my page.

On doing this:- I get the following output:-

 print_r($content);


Array
(
    [field_image_title] => Array
        (
            [#theme] => field
            [#weight] => 0
            [#title] => Image Title
            [#access] => 1
            [#label_display] => hidden
            [#view_mode] => full
            [#language] => und
            [#field_name] => field_image_title
            [#field_type] => text
            [#field_translatable] => 0
            [#entity_type] => field_collection_item
            [#bundle] => field_image_collection
            [#object] => FieldCollectionItemEntity Object
                (
                    [fieldInfo:protected] => 
                    [hostEntity:protected] => stdClass Object
                        (
                            [vid] => 119
                            [uid] => 1
                            [title] => My Page Name
                            [log] => 
                            [status] => 1
                            [comment] => 1
                            [promote] => 0
                            [sticky] => 0
                            [vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
                            [nid] => 119
                            [type] => subpage
                            [language] => und
                            [created] => 1408621327
                            [changed] => 1408640191
                            [tnid] => 0
                            [translate] => 0
                            [uuid] => 39145013-6637-4062-96e7-1b4589609c4f
                            [revision_timestamp] => 1408640191
                            [revision_uid] => 1

回答1:

what your are doing with this:

return $this-> $var;

is accessing a property named after what is contained in your $var variable which does not contain anything in the scope where it is defined. pass it as a function argument:

function accessObjectArray($var){
  return $this-> $var;
}

print $mything->accessObjectArray('vid');

but in any event, that won't work either since (as mentioned by @MikeBrant) you have an object in your parent object properties. something like this might work better

$o = new FieldCollectionItemEntity() // assumes this will construct the object in the state you have posted it
$o->accessObjectArray('hostEntity')->accessObjectArray('vid');

note that the method accessObjectArray($var) must be defined in both objects for this to work


the idea of a protected property is to prevent what you want to actually happen. But! protected means that only the class and it's extending classes can access a value. Make your own class that extends the other one:

class myClass extends FieldCollectionItemEntity {

      function accessParentProtectedVars($var){
        return $this->hostEntity->$var;
      }
      //other member functions
  }

then your accessObjectArray() function will be able to acces the protected property. note that it's hardcoded to access the hostEntity object.

but seriously, you may want to consult the creator of the other class and maybe you will devise a way to best manage this. My proposed solution is not that much of a good practice if I daresay.



回答2:

"$this-> $var;" this mean variable variable, and this throw php notice undefined variable $var,

you have to use

return $this->var; 

or

return $this->vid


回答3:

Answer for Drupal members as rendered array in the question looks like Drupal array

I believe you don't need a new class at all, you only need to get node's objects. So, below one line will work for you.

$parent_node = menu_get_object();

Now, you can access by $parent_node->vid