nitrag Posted February 10, 2010 Share Posted February 10, 2010 So I recently discovered variable reflection and it's quite handy. Normally I would have $group1, $group2, $group3...etc. But I want to create a foreach loop that gets that ending number and creates the different $groupX from an array. // The below outputs $group1 like I want it to $GRP = "group$groupID"; echo $$GRP; //the below does not work $grpcount = "1"; foreach($user->groupList as $groupID){ $GRP = "group$grpcount"; $$GRP = new Group(); $$GRP->setGroup($groupID); $$GRP->setUser($user->UID); $$GRP->setTab($grpcount); $$GRP->getWidgets(); $$GRP->writeProfile(); $$GRP->writeFullTab(); $$GRP->tab_output; $grpcount++; } Is it because of the foreach or because I cannot set/create a PHP Class with variable reflection. Thanks! Link to comment https://forums.phpfreaks.com/topic/191667-variable-reflection-not-working-when-setting-class/ Share on other sites More sharing options...
jl5501 Posted February 10, 2010 Share Posted February 10, 2010 I am not sure what you are trying to achieve here with your use of variable variables, but it should be covered by http://php.net/manual/en/language.variables.variable.php Link to comment https://forums.phpfreaks.com/topic/191667-variable-reflection-not-working-when-setting-class/#findComment-1010312 Share on other sites More sharing options...
nitrag Posted February 10, 2010 Author Share Posted February 10, 2010 if the below outputs correctly $groupcount = '1'; $group1 = 'Yes I work'; $GRP = "group$grpcount"; echo $$GRP; How do I create a class with variable variables. Instead of: $group1 = new Group(); I want to do this same thing with an array of groupX's. Because my user's may have multiple groups and each one needs an instance. $$GRP = new Group(); //I want the above to be the same as: //$group1 = new Group(); //but it's not returning anything Link to comment https://forums.phpfreaks.com/topic/191667-variable-reflection-not-working-when-setting-class/#findComment-1010324 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 But I want to create a foreach loop that gets that ending number and creates the different $groupX from an array. Firstly, you never iterate over any number in your code, you simply set $grpcount to 1 and keep using it. Secondly, variables variables are a known bottle neck to performance, much better off using an array. eg; $objects = array(); foreach (range(1,5) as $number) { $objects[$number] = new Group; } Link to comment https://forums.phpfreaks.com/topic/191667-variable-reflection-not-working-when-setting-class/#findComment-1010338 Share on other sites More sharing options...
nitrag Posted February 10, 2010 Author Share Posted February 10, 2010 Got it working! Thanks all for the insight! Link to comment https://forums.phpfreaks.com/topic/191667-variable-reflection-not-working-when-setting-class/#findComment-1010403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.