Yii2 组件将数据传递给 __construct [英] Yii2 component pass data to __construct

查看:42
本文介绍了Yii2 组件将数据传递给 __construct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要用作组件的库.在配置文件中我这样设置:

'components' =>['superLib' =>['类' =>'超级图书馆'//'__construct' =>[$first, $second] 也许 Yii 2 有这个属性],],

如何将数据传递给 __construct()?

解决方案

大多数时候你不必重写 __construct().

Yii 2 中的几乎每个对象都是从 yii\base\ 扩展而来的对象通过配置数组特征具有赋值属性.

Components 扩展自 yii\base\Component,后者也是从 yii\base\Object 扩展而来的.因此,在您的示例和类名中(请注意,您应该提供带有命名空间的完整类名,而在您的示例中它位于根命名空间中),您可以传递任何属性/值对:

'components' =>['superLib' =>['类' =>'超级图书馆''firstProperty' =>'第一个属性值','第二属性' =>'第二个属性值',],],

有时你需要使用 init() 方法(例如用于检查值是否具有有效类型并抛出某种异常,用于设置默认值等):

公共函数init(){父::init();//调用父实现;...}

以下是官方文档中的一些有用信息:

<块引用>

除了属性特性,Object还引入了一个重要的对象初始化生命周期.特别是创建一个新的Object 或其派生类的实例将涉及以下内容生命周期顺序:

  • 调用类构造函数;
  • 对象属性根据给定的配置进行初始化;
  • init() 方法被调用.

在上面,第 2 步和第 3 步都发生在类的末尾构造函数.建议您执行对象初始化在 init() 方法中,因为在那个阶段,对象配置已经申请了.

为了保证上面的生命周期,如果一个Object的子类需要覆盖构造函数,它应该像以下:

public function __construct($param1, $param2, ..., $config = []){...parent::__construct($config);}

<块引用>

也就是说,$config 参数(默认为 [])应该声明为构造函数的最后一个参数,以及父实现应该在构造函数的末尾调用.

如果仍然想在 __construct 中使用额外的参数,你可以这样做:

'components' =>['superLib' =>['类' =>'app\components\SuperLib',['firstParamValue', 'secondParamValue'],],],

你可以在官方文档中找到它这里 在第三个例子中.

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:

'components' => [
    'superLib' => [
        'class' => 'SuperLib'
        'firstProperty' => 'firstPropertyValue',
        'secondProperty' => 'secondPropertyValue',
    ],
],

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.):

public function init()
{
    parent::init(); // Call parent implementation;

    ...
}

Here is some useful info from official docs:

Besides the property feature, Object also introduces an important object initialization life cycle. In particular, creating an new instance of Object or its derived class will involve the following life cycles sequentially:

  • the class constructor is invoked;
  • object properties are initialized according to the given configuration;
  • the init() method is invoked.

In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that you perform object initialization in the init() method because at that stage, the object configuration is already applied.

In order to ensure the above life cycles, if a child class of Object needs to override the constructor, it should be done like the following:

public function __construct($param1, $param2, ..., $config = [])
{
    ...

    parent::__construct($config); 
}

That is, a $config parameter (defaults to []) should be declared as the last parameter of the constructor, and the parent implementation should be called at the end of the constructor.

If nevertheless want to use additional parameters in __construct you can do it like that:

'components' => [
    'superLib' => [
        'class' => 'app\components\SuperLib',
        ['firstParamValue', 'secondParamValue'],
    ],
],

You can find it in official docs here in third example.

这篇关于Yii2 组件将数据传递给 __construct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆