I
I
Ilya Karavaev2018-03-23 12:30:17
Yii
Ilya Karavaev, 2018-03-23 12:30:17

How to push an array into DI in Yii2?

I can not understand how to solve the issue with DI. An array in dependencies is REQUIRED. But there is also a class that must be taken from the container. I'm trying to do it in different ways, but this question has not won yet. Maybe someone knows?

// PageCountry прописан в контейнере и берется от туда без проблем
class PageValue {
    public function __construct(PageCountry $country, $values) {}
}
$value = \Yii::$container->get(PageValue::class, [], [$value]);
$value = \Yii::$container->get(PageValue::class, [$value]);

How to correctly pass parameters to a dependency?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitaichik, 2018-03-23
@mitaichik

Everything is written there in the comments to the get method. Well, or it's enough to dig into the code a bit:
\Yii::$container->get - the second parameter - the parameters that are passed to the constructor (Yii also supplements them with dependencies).
Inside the container calls the php.net/manual/ru/reflectionclass.newinstanceargs.php
method Accordingly, judging by the documentation of this method, you need to write

$value = \Yii::$container->get(PageValue::class, [$country, $value]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question