will it affect the website performance a lot?
This can't be answered the way you ask specifically on Stackoverflow. Especially the part about "a lot". You can however answer it by comparing the metrics running your different approaches in your environment(s).
Is there an alternative or easier way?
Sure, as so often. Most likely - and not only foremost for performance reasons - you'll run into a more fundamental problem (which will affect the performance sooner or later).
I say so because your "Constructor does Real Work" based on the fact it contains code with the new
keyword.
With the consequences that it wires (hard-encodes) the products class with the other class names (prevents changes) and comes with the object creation overhead of each of those classes (that is loading and initializing these objects in PHP and running their __construct
methods) - always!
Albeit your products may (now) depend on some of those classes (in the sense that it needs instances of these to work), it actually only need those instances to work (functionality) and the class-names are only needed to instantiate those objects.
Therefore a common procedure is to separate object creation from object usage.
This can be done by creating a different function or method that creates a products object passing the needed instances to the constructor via parameters.
A simplified example would be for a creation function:
function create_products_out_of_thin_air(): products
{
return new products(
new Class_1(),
new Class_2(),
new Class_3(),
new Class_4(),
new Class_5(),
new Class_6(),
new Class_7(),
new Class_8(),
new Class_9(),
new Class_10(),
new Class_11(),
new Class_12(),
new Class_13(),
);
}
And the class/constructor:
class products
{
public function __construct(
public object $class_1,
public object $class_2,
public object $class_3,
public object $class_4,
public object $class_5,
public object $class_6,
public object $class_7,
public object $class_8,
public object $class_9,
public object $class_10,
public object $class_11,
public object $class_12,
public object $class_13,
) {
}
}
When in need of products:
$products = create_products_out_of_thin_air();
Now the creation of products has been separated from its implementation which affects use incl. testing offering room for changes.
It is barely simplified (like in your question, here using Constructor Promotion) but should show how it works.
This at least should make the code more maintainable which allows you to deal with performance bottlenecks when you encounter them. E.g. when your metrics show that creating these classes affects performance a lot.
The example also perhaps already shows that products might already have too many dependencies. If that is the case, consider to only instantiate the objects the code actually depends on. Separating object construction from use as shown above will help you with that (compare: Lazy Loading, Object Factory, Factory Method, Dependency Injection, and Writing Testable Code).
Still the question about performance depends too much on the concrete implementation to be answered in abstract here.
Further references: