Jump to content

How to use variable variables of object?


Recommended Posts

I know how to use $$ and ${} to use variable variables, but how would I get the same functionality out of a variable that is storing an object name?


I have a function that is passed the name of an array of objects.  As you can guess, that name will change based on which portion of the program is calling

the function.  How would I use variable variables of objects in this instance?  Would it just be:


$this->${$objArrayName}->threadName = etc;




To make sure I'm not being misunderstood, I have 3 arrays of objects (replies, mainThreads, and stickies) and depending on which name I pass the function, I want it

to use $this->replies OR $this->mainThreads OR $this->stickies to apply properties of those objects to.


Thanks for any help.



Link to comment
Share on other sites

I wouldn't think so, the same way that:


$foo = 'bar';
$bar = '1234';
echo $foo; // this would echo 'bar'
echo ${$foo}; // this would echo '1234'
// at least as far as I understand it


Or am I wrong about this?  I guess I can check after I get home from work, can't do it here.

Link to comment
Share on other sites

Maybe you can post a var_dump() of your array structure, as I'm not clear on what it is.  "Variable properties" do work, such as $propertyname = 'bar'; $instance->$propertyname being resolved to $instance->bar

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.