-3

As of my observation, core team used the object manager as added

Example flow taken from module-cms

    public function __construct(
          //.....
          \Magento\Framework\ObjectManagerInterface $objectManager
          //.....
               ) 
                 {
                    $this->_objectManager = $objectManager;
                 }

Later used

$resultPage = $this->_objectManager->get('Magento\Cms\Helper\Page')->prepareResultPage($this, $pageId);
  1. Is it recommended to use object manager like this ?
  2. Why core team not passing this class directly to constructor as a dependency ?
  3. Is there any specific case/reason to use the object manager like this ?

Can any one provide the detailed answer. Thanks.

Yes some of the answer can be found in the link but my questions/ambiguity not solved yet.

7
  • As well as Why core team not passing this class directly to constructor as a dependency ? Is there any specific case/reason to use the object manager like this ? Oct 31, 2016 at 6:55
  • Can you please tell me Why it is duplicate ? I have not got straight answer for "Is it recommended to use object manager like this ?" Oct 31, 2016 at 7:00
  • using it like this is like using it directly the reasons are listed in the duplicate question Oct 31, 2016 at 7:06
  • 1
    Yes some of the answer can be found in the link but my questions/ambiguity not solved yet. Oct 31, 2016 at 7:08
  • 4
    Short answer: Just because it's in the core it doesn't make it right. Not all the core code is refactored yet to comply with the best practices. You should not use it. You should inject the OM only in factory classes.
    – Marius
    Oct 31, 2016 at 7:45

1 Answer 1

1

This question is clearly a duplicate but it seems like you need some clarifications.

If you do this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);

Or if you inject the object manager in your constructor and later use it by calling:

$this->_objectManager->create('Magento\Catalog\Model\Product')->load($id);

It's exactly the same you're using the OM directly so you'll get the answers you need from the duplicate.

The exceptions are:

  • in static magic methods like __wakeup, serialize, etc
  • in case you should make backward compatibility of constructor
  • in global scope, like in fixtures of integration test.
  • in class that needs only for creation of object like factory, proxy , etc

However, as stated in one of the duplicate answers:

M2 is a port of M1 - not a complete rewrite. So don't assume that all the M2 code is perfectly ported yet (unfortunately). Just because you find something in the M2 code base, that does not mean "its the best way to do it". Sometimes it is just "we have not got around to fixing it yet".

In your particular case, I reckon that's your answer, this code has not been refactored yet, it's a leftover from when M1 was ported to M2

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