Jump to content

variable reflection not working when setting class?


nitrag

Recommended Posts

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!

 

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

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;
}

Archived

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.