NeoMarine Posted November 21, 2008 Share Posted November 21, 2008 Is it possible to convert $this -> variableName to $variableName? I want to somehow loop through all the variables in scope (get_defined_vars()) and have the variables set in as $variableName so I can then just type: echo $variableName instead of echo $this->$variableName thanks Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/ Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 $variableName = $this->variableName; Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695616 Share on other sites More sharing options...
Maq Posted November 21, 2008 Share Posted November 21, 2008 You can learn a lot through trial and error Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695620 Share on other sites More sharing options...
xangelo Posted November 21, 2008 Share Posted November 21, 2008 Actually guys, it sounds like he wants them dynamically generated. Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695636 Share on other sites More sharing options...
NeoMarine Posted November 21, 2008 Author Share Posted November 21, 2008 that's right, dynamically! but how? I tried a foreach loop with get_defined_vars but that doesn't seem to work Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695641 Share on other sites More sharing options...
Mchl Posted November 21, 2008 Share Posted November 21, 2008 I'd try like this foreach($this as $property => $value) { $$property = $value } not tested Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695656 Share on other sites More sharing options...
NeoMarine Posted November 21, 2008 Author Share Posted November 21, 2008 Wow, it worked! Thanks a bunch! one more Question though, what the heck is $$ (double $) for $$property, when I took that out it failed Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695688 Share on other sites More sharing options...
Mchl Posted November 21, 2008 Share Posted November 21, 2008 This is how you define variable variables Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695696 Share on other sites More sharing options...
NeoMarine Posted November 21, 2008 Author Share Posted November 21, 2008 Awesome, I was wondering how to do that.. thanks Quote Link to comment https://forums.phpfreaks.com/topic/133684-convert-this-variablename-to-variablename/#findComment-695703 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.