0

I want to generate a class instance for each post value.

Something like this

$i=0;

foreach ($_POST['URL'] as $url) {
    $classInstance.$i = new className();
    $i++;
}

Each with a different name.

I read about variable variable names like:

${"classInstance" . $i} = new className(); 

But it's been previously discouraged in other questions (PHP Variables - Concatenate variable name)

Is there any other way to achieve the same without using variable variable names?

Community
  • 1
  • 1
user3808307
  • 2,270
  • 9
  • 45
  • 99

1 Answers1

1

The way you're proposing is perfectly valid. But there are not many justified use cases for using that piece of functionality for simply concatenating numbers to variables. If you need a collection of variables, simply aggregate those objects into a data structure such as an array or an SPL List, and manipulate them from there.

hasumedic
  • 2,139
  • 12
  • 17