flaab Posted May 24, 2007 Share Posted May 24, 2007 Hi everyone =) I'll go straight to the problem. I want to copy the vars from an object to normal variables with the same name as its attributes, whatever the object is and outside of it. Suppose we have an object like this: Object Foo var $var1 = Whatever1 var $var2 = Whatever2 var $var3 = Whatever3 var $var4 = Whatever4 End object And i want to create normal global variables from it like this... $var1 = Whatever1 $var2 = Whatever2 $var3 = Whatever3 $var4 = Whatvere4 I've tried the following but it doesn' t work: foreach($objet as $key => $value) { $key = $value; } How can i do that? It has to work for any object with it's attributes public Thnaks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/ Share on other sites More sharing options...
pocobueno1388 Posted May 24, 2007 Share Posted May 24, 2007 This is kinda a javascript thing, so I can't help you much. You do have "object" spelled wrong in your foreach loop though...that could very well be the problem. Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/#findComment-260810 Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 try $var1 = $Foo->var1; Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/#findComment-260813 Share on other sites More sharing options...
trq Posted May 24, 2007 Share Posted May 24, 2007 <?php $object = new yourclass(); foreach($object as $key => $val) { $k = $key; $$k = $val; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/#findComment-260853 Share on other sites More sharing options...
flaab Posted May 24, 2007 Author Share Posted May 24, 2007 Wow! Amazing! Thanks a lot =) Love u! Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/#findComment-260873 Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 PLease click the Solved Button (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/52836-solved-automatic-creation-of-vars-from-an-object/#findComment-260876 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.