Why Magento is using $this->_objectManager->create() directly in function _initCategory in file vendor/magento/module-catalog/Controller/Adminhtml/Category.php?

protected function _initCategory($getRootInstead = false)
        $categoryId = $this->resolveCategoryId();
        $storeId = (int)$this->getRequest()->getParam('store');
        $category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);

        if ($categoryId) {
            if ($storeId) {
                $rootId = $this->_objectManager->get(
                if (!in_array($rootId, $category->getPathIds())) {
                    // load root category instead wrong one
                    if ($getRootInstead) {
                    } else {
                        return false;

        $this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
        $this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
        return $category;

As per the Magento recommendation, we should include class/object in constructor using DI. So, why is it so?

Or Am I reading this code incorrectly?


1 Answer 1


They were using objectManager before DI constructor was ready. Now there is no time to fix all usages of object manager. But if you are writing new code you should always use DI constructor.

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