2

I've a class located in \Vendor\Module\Model which is named Test. Here is the constructor of this class :

public function __construct(array $map = [])
{
    var_dump($map);
    die;
}

I created a di.xml located in \OtherVendor\OtherModule\etc\di.xml I put in :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <type name="Vendor\Module\Model\Test">
        <arguments>
            <argument name="map" xsi:type="array">
                <item name="test" xsi:type="array">
                    <item name="test" xsi:type="string">test</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Do you have an idea why on my constructor $map is empty?


Here is the code of my class :

<?php

namespace Test\ImportExport\Model\Import;

class SourceConverterAdapter
{
    /**
     * @var array
     */
    protected $_map;

    /**
     * Factory constructor
     *
     * @param array $map
     */
    public function __construct(array $map = [])
    {
        $this->_map = $map;
    }

    public function process($needle)
    {
        var_dump($this->_map);
        return 1;
    }
}


<type name="Test\ImportExport\Model\Import\SourceConverterAdapter">
        <arguments>
            <argument name="map" xsi:type="array">
                <item name="test" xsi:type="string">hello</item>
            </argument>
        </arguments>
    </type>
6
  • add your OtherVendor\OtherModule\Model\Test code in question – Rutvee Sojitra May 29 '19 at 8:22
  • I replaced <item name="test" xsi:type="string">test</item> and it's the same. Then it doesn't come from the other class – help May 29 '19 at 8:28
  • Did you cleared cache? – Sohel Rana May 29 '19 at 8:30
  • I've all caches disabled, i run a setup:di:compile and i'm in developer mode. Have you reproduced ? – help May 29 '19 at 8:33
  • Change __construct(array $map = []) to __construct(array $map ) and can try with private $map; public function __construct(array $map) { $this->map = $map; } – Amit Bera May 29 '19 at 8:50
0

Variable name in constructor resides in the global scope as part of constructor signature so while referring to your param you need to provide the same variable name, otherwise, it will just take up default value you've provided in the constructor. Change di.xml as follows;

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <type name="Vendor\Module\Model\Test">
        <arguments>
            <argument name="map" xsi:type="array">
                <item name="test" xsi:type="array">
                    <item name="test" xsi:type="string">test</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

EDIT : Just realized you've written right config. Just FYI di compile triggers recompiles the di.xml in all the registered modules that's why it may not be reflecting before.

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.