5

For example i have following two classes.

<?php
namespace Namespace\Modulename\Model;
class Message 
{
    public function getMessage()
    {
        return 'Hello World!';
    }
}
?>

<?php
namespace Namespace\Modulename\Model;

class Example
{
    protected $messageObject;
    public function __construct(Message $messageObject)
    {
    $this->messageObject = $messageObject;
    }

    public function sendHelloAgainMessage()
    {
      return $this->messageObject->getMessage();    
    }
}
?>

I dont need to create below xml in di.xml.because objectmanager take care creation of object automatically from constructor type(means here Message)

 <type name="Namespace\Modulename\Model\Example">
            <arguments>
                <argument name="messageObject" xsi:type="object">Namespace\Modulename\Model\Message</argument>
            </arguments>
    </type>

But my doubt is in which time i have to create above xml?

update

for example please look at <type name="Magento\Catalog\Helper\Product"> di.xml in catalog module.

<argument name="catalogSession" xsi:type="object">Magento\Catalog\Model\Session\Proxy</argument>

if i give directly that class name(Magento\Catalog\Model\Session\Proxy) in constructor instead of in xml?

public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Session\Proxy $catalogSession,
8

Good practice is to always request for interface in constructor. This way every developer will be able to create totally different implementation of that interface without extending the default implementation.

But when OM encounters constructor that requests some interface it (OM) does't know which exactly implementation of that interface should be provided.

And a way to let it know is to write preference configuration in di.xml:

<config>
    <preference for='MessageInterface" type="Message" />
</config>

Other scenario is when some 3rd party developer wants to substitute default implementation of message in your example:

<config>
    <type name="My\Module\Model\Example">
        <arguments>
            <argument name="messageObject" xsi:type="object">Other\Module\Message</argument>
        </arguments>
    </type>
</config>
  • thank you for your response.the first preference xml & interface concept i understood clearly.but later one(arguments) even magento core team also used(as u said 3rd developers). – Sivakumar K Jul 21 '15 at 12:37
  • It was just as example for 3rd party developer. Sure, Magento core team uses this in some cases where one module is supposed to replace implementation of other module – Anton Kril Jul 21 '15 at 22:21
  • So that means if a module is implemented to change the behavior then we need to define it using <type> and when there is a totally external approach then we need to use interfaces, correct me if I am wrong. – Chiragit007 Sep 28 '15 at 12:47

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.