0

In Magento 2 we are using dependency injection in replace of old style getModel, getHelper etc

I have a particular class that i find i am injecting in a lot of places which holds some general configuration values i need across lots of classes.

I want to know how to make this a singleton like in Magento 1 we did getSingleton.

1

if you don't explicitly configure it otherwise, the same object is shared everywhere. So basically, the same as a getSingletion in magento1

You can still configure constructor arguments not to be shared (if needed) in the di.xml like described here: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/di-xml-file.html

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Filesystem" shared="false">
    <arguments>
        <argument name="adapter" xsi:type="object" shared="false">Magento\Filesystem\Adapter\Local</argument>
    </arguments>
</type>

in most cases, however, I think a factory is more suited to create a new object inside your class

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.