0

I have a class called products. And I need to initialize at least 12 new classes inside the __construct function of this class. If I run it as below, will it affect the website performance a lot? Is there an alternative or easier way?

Note: My PHP version is v8

For example:

<?php
class products(){

    function __construct(){
        $this->class_1 = new Class_1();
        $this->class_2 = new Class_2();
        $this->class_3 = new Class_3();
        $this->class_4 = new Class_4();
        $this->class_5 = new Class_5();
        $this->class_6 = new Class_6();
        $this->class_7 = new Class_7();
        $this->class_8 = new Class_8();
        $this->class_9 = new Class_9();
        $this->class_10 = new Class_10();
        $this->class_11 = new Class_11();
        $this->class_12 = new Class_12();
        $this->class_13 = new Class_13();
    }

}
?>
  • _"will it affect the website performance a lot?"_ - that depends and it requires that you run metrics on your own and then compare the outcomes. I could not tell you. As you're concerned about performance, `new` will call the class's `__construct` method, so if it does a lot, there is an effect, and as there often is one class defined in a single file the autoloading might create disk i/o. Keep inheritance (_extends_, _implements_, _use_) in mind, as this can trigger the autoloader for more than one file per class. – hakre Oct 03 '21 at 14:16
  • 1
    Of course there are alternatives. There always are. However, your question is very abstract. We have no idea why you need to initialize 12 new classes. It could be that five of them are the same simple class to represent an amount of money. In that case not many resources are used. On the other hand there could be one class that needs an external API calls to work, and it could slow everything down and make it unreliable. Whether performance is severely affected depends on what these classes do, and you haven't told us. – KIKO Software Oct 03 '21 at 14:22
  • 1) product_categories(); 2) product_descriptions(); (for different languages. like opencart) 3) class_taxes(); 4) class_stock_statuses(); 5) class_length(); 6) class_weight(); 7) class_manufacturers(); 8) class_features(); 9) class_options(); 10) class_recurrings(); 11) class_users_groups(); 12) class_users(); 13) class_recur_payments(); Each class is used to manage its own database and process the pulled databases. – Batuhan Polat Oct 03 '21 at 14:30
  • 1
    How much is "a lot"? What did you measure? Sorry, but this is not a question that can be answered without mixing opinions and guesses into the answer, which makes it a bad question for Stack Overflow. Also, you are asking two questions actually. That said: Consider creating these additional objects on demand. – Ulrich Eckhardt Oct 03 '21 at 14:32

1 Answers1

1

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:

hakre
  • 193,403
  • 52
  • 435
  • 836