Answer the question
In order to leave comments, you need to log in
How to create a certain number of objects?
Let's say there are 2 classes
class Apple
{
}
class Orange
{
}
new Apple()
new Apple()
new Apple()
new Apple()
Answer the question
In order to leave comments, you need to log in
Somehow fake the loop with, say, arrays?
$length = 10;
array_map(fn() => new Apple(), array_fill(0, $length, NULL));
function makeMany(string $className, int $quantity)
{
return array_map(fn() => new $className(), array_fill(0, $quantity, NULL));
}
// использование
makeMany("Apple", 10);
makeMany("Orange", 20);
// или
makeMany(Apple::class, 10);
makeMany(Orange::class, 20);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question