ObjectManager usage directly

Should not use Object manager directly!

For instance:

MagentoFrameworkAppObjectManager::getInstance();

also if you are working with event observers or plugins, you should never use it directly.

You could use it in Factories, but except that you should inject the Object Manager in the Constructor first then you can use its object in your method

Preferred to use:

1) declare private object:

private $_objectManager;

2) inject in the constructor and initialize:

public function __construct(
    MagentoFrameworkObjectManagerInterface $objectmanager
) {
    $this->_objectManager = $objectmanager;
}

3) use in some method:

public function create() {
    return $this->_objectManager->create(/* ......... */);
}

Leave a comment

Your email address will not be published. Required fields are marked *