What is the difference between __construct
and init()
in Yii2 models?
For example:
$model = new Model()
Will __construct
and init()
called here? If so, what is the difference between them?
What is the difference between __construct
and init()
in Yii2 models?
For example:
$model = new Model()
Will __construct
and init()
called here? If so, what is the difference between them?
qiang:
This is the case for init ().
With an init () method, it is possible that it is instantiated while before fully initialized. For example, an application component could be configured using app config. If you’re overriding its init () method, you’ll be able to check if everything is ready. Similar things happen to a widget and other configurable components.
Even if init () is called within constructor rather than by another object, it has meaning. For example, in CApplication, there are preInit () and init (). They may be overridden so that the custom cycles only occur at the expected life cycles.
I agree that you are very important. This is a method that has already been configured.
http://www.yiiframework.com/forum/index.php/topic/3884-init-or-contruct/
init allows you to extend the functionality of components. Safely configure the application component by redefining init, without fear of disrupting the logic of the constructor, and also ensure that the component is ready for use.
Source: https://ru.stackoverflow.com/questions/445964/
All Articles