I have class in my module for Magento 2. His constructor gets 2 parameters

class MyClass
    private $str1;
    private $str2;

    public function __construct(string $str1, string str2)
        $this->str1 = $str1;
        $this->str2 = $str2;

This parameters saved in database in core_config_data table. I want use this class in other module classes, but i dont know how i can use DI for object transfer of this class in constructors others classes. I can use for this di.xml?

  • Hard to understand the question, can you give more specific example? – Eugene Tulika Apr 3 '18 at 12:39
  • class ObserverClass extends Observer { public function __construct (MyClass $ myclass) {} } – Akolzin Dmitry Apr 3 '18 at 13:57
  • For example exist observer class and I want use my class in observer, like this. But dependency injection will not create this object, as dont know what parameters must get my class – Akolzin Dmitry Apr 3 '18 at 14:01
  • I can create object through $myclass = new MyClass($str1, $str2); but this a bad practice in Magento – Akolzin Dmitry Apr 3 '18 at 14:04
  • Please visit: magento.stackexchange.com/questions/147366/… this may help you – iqbal malik Mar 1 '19 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.