Raheel
Raheel

Reputation: 9024

Calling methods at object creation via constructor performance effect PHP

My question is what is the best way in terms of performance. For example in the application bootstrap process i have a class

<?php 
class Application
{
    public function __construct()
    {
        $this->setErrorHandler();
        $this->setDatabase();
        $this->sessionHandler();
        $this->disptachRequest();
    }

}
$app = new Application;

Is this a good approach or i should call these methods separately from the variable that holds the object?

I have set those function to private actually that is why i am calling them in constructor. Need guidance if this is good or bad?

Upvotes: 2

Views: 100

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

There is nothing inherently bad in it.

For performance, the question is, if all the calls are necessary in all cases. if not, you could defer them and execute them only if needed (this is called lazy loading)

For proper object oriented design the question is if the Application class and all its methods serve a common single responsibility. If not, break it down into smaller classes.

Upvotes: 1

Related Questions