I have a lib which I want to use as component. In config file I set it like that:
'components' => [
'superLib' => [
'class' => 'SuperLib'
// '__construct' => [$first, $second] Maybe Yii 2 have property for this
],
],
How I can pass data to __construct()
?
Most of the times you don't have to override
__construct()
.Pretty much every object in Yii 2 is extended from yii\base\Object which has assignment properties through configurational array feature.
Components is extended from yii\base\Component, the latter is extended from
yii\base\Object
too. So in your example along with class name (note that you should provide full class name with namespace while in your example it's in the root namespace) you can pass any properties / values pairs:Sometimes you need to use init() method (for example for checking if values have valid types and throw some kind of exceptions, for setting default values, etc.):
Here is some useful info from official docs:
If nevertheless want to use additional parameters in
__construct
you can do it like that:You can find it in official docs here in third example.