Dependency injection

Until now, we have seen how the object manager has control over the instantiation of dependencies. However, by convention, the object manager isn't supposed to be used directly in Magento. Rather, it should be used for system-level things that bootstrap Magento. We are encouraged to use the module's etc/di.xml file to instantiate objects.

Let's dissect one of the existing di.xml entries, such as the one found under the vendor/magento/module-admin-notification/etc/adminhtml/di.xml file for the Magento\Framework\Notification\MessageList type:

<type name="Magento\Framework\Notification\MessageList"> <arguments> <argument name="messages" xsi:type="array"> <item name="baseurl" xsi:type="string"> Magento\AdminNotification\Model\System ...

Get Magento 2 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.