Is using Object Manager in .PHTML files is good practise ?

object-manager
(sany) #1

Using object manager in PHTML files directly good practice or not, if not then why ?

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

0 Likes

(Dmitry Fedyuk) #2

Everybody says that it is, but I think it is generally not an issue.
There is a small issue that the such code is longer, so for a long-term project I recomment to refactor it.
Also, your code has a much bigger issue than Object Manager: you loads a product from the database directly.
It is very bad: it hurts the Magento’s performance.
Learn about object repositories.

0 Likes