This question already has an answer here:

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 ?

marked as duplicate by 7ochem, Rakesh Jesadiya, Murtuza Zabuawala, Marius magento2 Feb 9 '17 at 12:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 13 down vote accepted

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.

  • 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 Jul 19 '16 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 Jul 20 '16 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 – Pradeep Kumar Oct 27 '17 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 Oct 28 '17 at 20:39

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