cancel
Showing results for 
Search instead for 
Did you mean: 

Managing objects in a class without constructor

Occasional Contributor

Managing objects in a class without constructor

Hi everyone,

 

Magento 2 best practices say that dependency injection should be preferred to the object manager when instantiating objects.

 

How are we supposed to manage objects in a class that doesn't have a constructor?

Should all classes absolutely have a constructor? If no, what the best practice when there is no constructor?

 

Thanks a lot for your answers.

 

2 REPLIES
Highlighted
Occasional Contributor

Re: Managing objects in a class without constructor

more specifically: is the object manager fine to use when a class has no constructor or is there a preferable way?

Occasional Contributor

Re: Managing objects in a class without constructor

seriously, it's hard to believe there won't be any answer to this question.

I am maintaining a M2 extension I didn't create. In that extension, I have a static class with static functions called statically all over the extension.

 

I need to call objects in the static class in order to perform some actions.

I read everywhere that the object manager shouldn't be used to instantiate objects.

 

Is it fine to use the object manager in this case, or maybe a way to use dependency injection, or is there another best practice?