What is the difference or best way for creation instance of new class?

Object manager?


Define in to constructor?


Using __construct() method defined inside php file,

Demo example,

protected $_entityResource;
public function __construct(
    \Magento\Catalog\Model\ResourceModel\AbstractResource $entityResource
) {
    $this->_entityResource = $entityResource;

Why we have to not used Object Manager inside file, please check community thread, ObjectManager


As per my opinion its better to use construtor instead of Object Manager. We should not use the ObjectManager directly! It defeasance the purpose of dependency injection. We can use ObjectManager, if there is no alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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