Categories


Archives


Recent Posts


Categories


Magento 2: Replacements for getModel and getSingleton

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Recent builds of Magento 2 have done away with the global Mage class. There are pros and cons to this, and I feel a more in depth blog post stirring in my gut, but that will need to wait for another time.

Of more immediate concern: without a Mage class, how does a developer instantiate model or magento-singleton object? The Mage::getModel and Mage::getSingleton methods have been retired, and in their place Magento has a new “Object Manager” object.

Right now this object manager is a PHP singleton, which you can grab with the following call.

$object_manager = MagentoCoreModelObjectManager::getInstance();

I say “right now” because from the looks of things, Magento is moving towards an architecture where this object will be automatically available in appropriate places as the _objectManager property.

$this->_objectManager

Regardless of how you grab an instance of the object manager, the two methods you’re interested in are get and create. The get method replaces getSingleton

$logger = $object_manager->get('MagentoCoreModelLogger');

If you take a look at the object manager source

#File: lib/Magento/ObjectManager/ObjectManager.php
/**
 * Retrieve cached object instance
 *
 * @param string $type
 * @return mixed
 */
public function get($type)
{
    $type = $this->_config->getPreference($type);
    if (!isset($this->_sharedInstances[$type])) {
        $this->_sharedInstances[$type] = $this->_factory->create($type);
    }
    return $this->_sharedInstances[$type];
}

You’ll see this implements a pattern very similar to the old Magento 1 getSingleton method, but that the doc block simply refers to this as a “cached object instance”.

As for the Mage::getModel method, this has been replaced by the object manager’s create method.

$url = $object_manager->create('MagentoCoreModelUrl');

Again, if you take a look at the source code

/**
 * Create new object instance
 *
 * @param string $type
 * @param array $arguments
 * @return mixed
 */
public function create($type, array $arguments = array())
{
    return $this->_factory->create($this->_config->getPreference($type), $arguments);
}

You’ll see in the doc block that this creates a new instance. You’ll also notice both get and create use the same PHP code to actually do their instantiating.

$this->_factory->create($type);

You’ll also notice the class alias strings are No_Longer_Underscore_Separated and instead they’re NamespaceSeparated

$logger = $object_manager->get('MagentoCoreModelLogger');
$url    = $object_manager->create('MagentoCoreModelUrl');

Lots to think about here.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 6th November 2013