in magento 1 it was possible to instatiate objects on the fly:

foreach($modelCustomerAccounts as $modelCustomerAccount){
            $modelCustomer = Mage::getModel($customerModelClass);
            $customers[] = $modelCustomer->load($modelCustomerAccount->getCustomerId());


  public function createObjectOnTheFly($modelName,$model)
        $newObject = Mage::getModel($modelName . "/$model");
        return $newObject;

However the preferred method to instatiates objects in magento 2 is via Dependency injection factories:

  public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        array $data = []
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);

my question is how can i now dynamically instantiate objects on the fly in magento 2 bearing in mind that its recommended that objects instantiation is done via a factory method passed through the constructor


You have included the answer in your question:

the preferred method to instatiate objects in Magento 2 is via Dependency Injection factories

Without getting too deeply into the mechanics of how Magento 2's DI system works, basically, including a class in the constructor means Magento will attempt to instantiate an object from that class and then you can assign that instance of the class to a class property in the constructor.

All instances of classes which you need to use in a class must be injected via the constructor.

There is a reason why the Mage 'God' class of Magento 1 was removed in Magento 2 - because it can create hidden dependencies and these can create bugs and weird behaviour which is hard to track down. It also makes unit testing harder and encourages poor software design and programming practice.

In summary then, there is no recommended way to create instances of arbitrary objects 'on the fly' and this is part of the very architecture of Magento 2.

(Yes, there is actually a method of doing this which is analogous to the Mage class of Magento 1, but I'm not going to mention it here because it's not recommended to be used in your code by Magento).

  • thank you for that. you confirmed what i already suspected
    – theSeeker
    Jun 22 '18 at 6:49

Another one "not recommended" method to instantiate an object "on-the-fly" is using Object Manager.

class Myclass
    protected $objectManager;
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
        $this->objectManager = $objectManager;

    public function getSomethingFromElsewhere($code)
        $object  = $this->objectManager->create(__CLASS__.'\\'.$code);

//      or like this
//      $object  = $this->objectManager->create(__CLASS__.'\\'.$code . 'Factory')->create();

        return $object->_someProperty;

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.