0

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
4
  • 3
    Can you show what you have done within the FieldCollectionItemEntity to try to make the contents of the protected property available? Creating some new class is not going to do anything to help you access a protected property in a different class. Commented Aug 21, 2014 at 19:51
  • Hi Mike, it's basically a pre-made module out of which I am trying to extract the variable. I hope I understood your question. If you want me to put all the output here, I can do that. Commented Aug 21, 2014 at 20:08
  • 2
    I think you might not be grasping what a protected object property is. You will not have any way to access it unless the class actually exposes a method to let you get at that property (or provide magic method getter functionality). Have you looked over the methods available to that class to see id one works? Commented Aug 21, 2014 at 20:19
  • oh. I see. Well then I can look into the documentation for this module. Thanks a lot :) Commented Aug 21, 2014 at 20:29

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

that's right, i used it as an example, just edit my comment to have vid property
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.

1 Comment

Hi Felix, thank you for your answer, I actually don't have access to the class itself. I only have a multi-dimensional variable within which I have to extract my variable. I updated my answer for your to take a look. Again, I am grateful for your assistance.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.