Hello, I would like to know if there is possible to inject string or number as parameter to class, for example I have some class with constructor:
public function __construct( \Magento\Framework\App\State $state, \Magento\Store\Model\App\Emulation $emulation, \Magento\Framework\ObjectManagerInterface $objectManager,
int $someIntegerValue ){ $this->state = $state; $this->emulation = $emulation; $this->objectManager = $objectManager; }
I don't know, how can I pass integer value to this. Is it possible?
Thanks,
Sebastian
Hello @Sebastian222
Yes, in Magento 2, you can inject string or number parameters into a class constructor.
Here's an example of how you can define a class with string and number parameters in its constructor:
public function __construct( \Magento\Framework\App\State $state, \Magento\Store\Model\App\Emulation $emulation, \Magento\Framework\ObjectManagerInterface $objectManager, int $someIntegerValue ){ $this->state = $state; $this->emulation = $emulation; $this->objectManager = $objectManager; $this->numberParam = $someIntegerValue; }
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
Hey,
You can add class arguments by adding dependencies in di.xml file. e.g.,
<type name="Vendor\Extension\Model\Class1"> <arguments> <argument name="arg1" xsi:type="object">Vendor\Extension\Model\Class2</argument> <argument name="arg2" xsi:type="string">testArgument</argument> ... </arguments> </type>
Hello @Sebastian222,
In Magento 2, passing scalar values (like integers or strings) directly via dependency injection is not straightforward because the DI configuration primarily supports objects and not scalar values. However, you can achieve this by using a combination of the DI configuration and a custom configuration provider to pass the scalar values.
here is the demo code to achieve this :
Create a configuration provider class that will hold your integer value.
namespace Vendor\Module\Model; class ConfigProvider { private $someIntegerValue; public function __construct(int $someIntegerValue) { $this->someIntegerValue = $someIntegerValue; } public function getSomeIntegerValue(): int { return $this->someIntegerValue; } }
In your module's di.xml file, configure the ConfigProvider class and pass the integer value as an argument.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Vendor\Module\Model\ConfigProvider"> <arguments> <argument name="someIntegerValue" xsi:type="number">123</argument> </arguments> </type> </config>
Update your class to accept the ConfigProvider instance and use it to get the integer value.
namespace Vendor\Module\Model; class YourClass { private $state; private $emulation; private $objectManager; private $someIntegerValue; public function __construct( \Magento\Framework\App\State $state, \Magento\Store\Model\App\Emulation $emulation, \Magento\Framework\ObjectManagerInterface $objectManager, ConfigProvider $configProvider ){ $this->state = $state; $this->emulation = $emulation; $this->objectManager = $objectManager; $this->someIntegerValue = $configProvider->getSomeIntegerValue(); } // Other methods... }
If the issue will be resolved, Click Kudos & Accept as a Solution.