How to mock static method call by constructor?

What are the reasons to implement the init() method when there is the constructor method in PHP?

  • I've seen many libraries that require to call the init() method before performing the functionality they provide. And often no parameter is passed to the method. I'm wondering why they design like so when it's possible to set up necessary property values in the constructor. If the user wants to reset the settings, what's wrong with re-instantiating the object?

  • Answer:

    I can't say for PHP.. However in java frameworks like Spring and EJB, There is a difference between the time an object being created, and the time it can start using a framework. When the framework loads, it starts creating all the objects that it needs. However, while it's creating objects, the framework is not completely ready yet, because it's not done loading everything that it needs to. There can be cases where the object needs to use the framework to initialize itself. It can't so it in the constructor because the framework is not ready. So, you add an init method that is called by the framework after the framework is ready

Jayesh Lalwani at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.