0

In magento2, we have use object manager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

and use get() and create() method

$cookieManager = $objectManager->get('{MyClassName}')

$cookieManager = $objectManager->create({MyClassName})

Now, i want to know

  • what is different between this two methods?
  • When and where we need to use this two methods?
0

1 Answer 1

1

=> create () method :

The create method will instantiate a new object each time it’s called.

Example :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Namespace\Modulename\Model\Modulename');

=> get() method :

The get method will instantiate an object once, and then future calls to get will return the same object.

Example :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->get('\Namespace\Modulename\Model\Modulename');

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