11

Magento 2 uses dependency injection means when ever client required any services we have to pass in constructor.

So magento take care like automatically create objects recursively..

but there is a statement like we should not use object manager. I'm very confusing on this part. Can you some one explain ?

0

1 Answer 1

20

There are several reasons. The code will work, but it is best practice to not reference the ObjectManager class directly.

  • Because we say so! ;-) (better expressed as consistent code is good code)
  • The code could by used with a different dependency injection framework in the future
  • Testing is easier - you pass in mock arguments for the required class, without having to provide a mock ObjectManager
  • It keeps dependencies clearer - it is obvious what the code depends on via constructor list, rather than having dependencies hidden in the middle of the code
  • It encourages programmers to think about concepts like encapsulation and modularization better - if the constructor gets big, maybe it is a sign the code needs refactoring

I would also suggest reading up on dependency injection. There are lots of tutorials around on the concept. Here are a few I found pretty quickly, with Martin Fowler's description being definitely worth a read.

At the end of the day just remember dependency injection is not a new concept - Magento is following best practices learnt over more than 10 years by the community at large.

4
  • What if you have a helper class of 10 functions used by different controllers and ONLY 1 function requires specific model? Wouldn't be redundant (from performance view) to load model through constructor injection for each of 10 functions while we could load it using object manager only inside 1 single function?
    – JohnyFree
    Commented Jul 19, 2016 at 20:15
  • 1
    Yes. Sometimes that is a sign of poorly factored code, sometimes it is reasonable - in which case consider using a factory class or similar. I generally discourage throwing methods into a helper class which are not related. It is better to have separate classes that represent real purposes. Or use static methods in which case there is no need for a constructor (the calling code is responsible to get handles to needed data structures).
    – Alan Kent
    Commented Jul 20, 2016 at 23:08
  • @AlanKent :- i have one situation where i have use object manger , so i need to implement the logic where function can be loads dynamically as like product type do (in default M2 product type use object manger to load the simple or virtual product object ), based on the param i have to load the class , if i pass all needed class in readers class in di.xml when ever reader is called it init all class but i should init only that specific class which sent on reader param in that case i have to use object manger , so what you suggest Commented Oct 27, 2017 at 8:46
  • Hi Pradeep - sorry, very hard to work out exactly what you are doing from your description. You could try joining the Magento Community Engineering slack and discuss with the team there so you can go into more detail. To join see community.magento.com/t5/Magento-DevBlog/…. It is intended for those who plan to contribute to Magento, but they may help with “advanced” questions there for more advanced use cases.
    – Alan Kent
    Commented Oct 28, 2017 at 20:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.