0

I use third-party library in my module. I want to create a class object of this library using the factory class. But the problem is that constructor is this class takes 3 arguments string types.

namespace VendorName\ModuleName\Block\Adminhtml\System\Config\Form\Field;
class Shipping extends \Magento\Config\Block\System\Config\Form\Field
{
private $systemStore;
private $formFactory;
private $config;
private $shippingConfig;

public function __construct(
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Framework\App\Config\ScopeConfigInterface $config,
    \Magento\Shipping\Model\Config $shippingConfig,
    \VendorName\ClassNameFactory $classNameFactory
) {
    $this->systemStore = $systemStore;
    $this->formFactory = $formFactory;
    $this->config = $config;
    $this->shippingConfig = $shippingConfig;
    $var = $classNameFactory->create([
        'param1' => 'value1',
        'param2' => 'value2',
        'param3' => 'value3'
    ]);
  }
}

And when I try create object like this I get error

main.CRITICAL: Warning: Missing argument 2 for Vendor\Class::__construct(), called in vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

2
  • can you please add full code of your file? Apr 9, 2018 at 14:29
  • Yes of course. I think that I should declare these arguments in di.xml, but I do not know how to do it correctly Apr 9, 2018 at 14:45

2 Answers 2

0

Try below code.

namespace VendorName\ModuleName\Block\Adminhtml\System\Config\Form\Field;
class Shipping extends \Magento\Config\Block\System\Config\Form\Field
{
private $systemStore;
private $formFactory;
private $config;
private $shippingConfig;
/**
 * @var NotEmpty
 */
private $var;

public function __construct(
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Framework\App\Config\ScopeConfigInterface $config,
    \Magento\Shipping\Model\Config $shippingConfig,
    \VendorName\ClassNameFactory $classNameFactory
) {
    $this->systemStore = $systemStore;
    $this->formFactory = $formFactory;
    $this->config = $config;
    $this->shippingConfig = $shippingConfig;
    $this->var = $classNameFactory->create([
        'param1' => 'value1',
        'param2' => 'value2',
        'param3' => 'value3'
    ]);
  }
}
3
  • Does not work, the error is still present Apr 9, 2018 at 14:59
  • because \VendorName\ClassNameFactory this path seems wrong. Apr 9, 2018 at 15:06
  • Why? Class \VendorName\ClassName exist and \VendorName\ClassNameFactory automatically generated Apr 9, 2018 at 16:56
0
namespace VendorName\ModuleName\MyClass;

class MyClassFactory
{
    public function create($param1, $param2, $param3)
    {
        return new \VendorName\ClassName($param1, $param2, $param3);
    }
}

I determined my factory class and use her

namespace VendorName\ModuleName\Block\Adminhtml\System\Config\Form\Field;
class Shipping extends \Magento\Config\Block\System\Config\Form\Field
{
private $systemStore;
private $formFactory;
private $config;
private $shippingConfig;

private $var;

public function __construct(
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Framework\App\Config\ScopeConfigInterface $config,
    \Magento\Shipping\Model\Config $shippingConfig,
    \VendorName\ModuleName\MyClass $classNameFactory
) {
    $this->systemStore = $systemStore;
    $this->formFactory = $formFactory;
    $this->config = $config;
    $this->shippingConfig = $shippingConfig;
    $this->var = $classNameFactory->create([
        $param1,
        $param2,
        $param3
    ]);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.