linus72982 Posted March 17, 2011 Share Posted March 17, 2011 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. -Adam Quote Link to comment https://forums.phpfreaks.com/topic/230873-how-to-use-variable-variables-of-object/ Share on other sites More sharing options...
btherl Posted March 17, 2011 Share Posted March 17, 2011 Does $this->$objArrayName work? Quote Link to comment https://forums.phpfreaks.com/topic/230873-how-to-use-variable-variables-of-object/#findComment-1188536 Share on other sites More sharing options...
linus72982 Posted March 17, 2011 Author Share Posted March 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230873-how-to-use-variable-variables-of-object/#findComment-1188648 Share on other sites More sharing options...
btherl Posted March 17, 2011 Share Posted March 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/230873-how-to-use-variable-variables-of-object/#findComment-1188865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.