init() is the method of any object that extends from yii\base\Object (and most of the objects extend from it).
From official docs:
In addition to the property of a property, Object also introduces an important object initialization life cycle. In particular, creating a new instance of an object or its derived class will include the following life cycles in sequence:
- the constructor of the class is called;
- object properties are initialized in accordance with a given configuration;
- The init () method is called.
In the above example, both steps 2 and 3 meet at the end of the constructor class. It is recommended to initialize the object in the init () method, since at this stage the configuration of the object is already applied.
It is recommended to use init() , you can even see it from the source code and extensions, but in some cases you can use __construct() . There are several recommendations for their implementation, you can find them on one page in official documents here .
__constuct is a native function of the PHP language, you can read more about this in the official PHP docs in this section .
source share