-1

Look at the last echo line and you will understand my question. I am so new to OOP and don't know how to ask this properly.

class myObject {
   function myObject ($obj1var, $obj2var){
      $this->obj1var= $obj1var;
      $this->obj2var= $obj2var;
   }
}

$obj1 = new myObject ("value1","value2");
$obj2 = new myObject ("value1","value2");

for($i=1; $i<=2; $i++){ 
   echo  ${'obj' . $i}->obj1var."&nbsp;"; //this works
   echo ${'obj' . $i}->obj.$i.var."&nbsp;"; //this normally doesn't work
}
2
  • Interesting but how people expect me to find older releated subjects without knowing the OOP well. Commented Sep 10, 2017 at 11:12
  • Learning how to best use PHP and StackOverflow is a journey. Sorry for the inconvenience. Commented Sep 10, 2017 at 11:46

1 Answer 1

-1

This is how you do that:

for($i=1; $i<=2; $i++){ 
   echo  ${'obj' . $i}->obj1var."&nbsp;"; //this works
   echo ${'obj' . $i}->{'obj'.$i.'var'}."&nbsp;"; //variable variable
}

It is called a variable property.

You are already using variable variables, this is very similar.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.