2

I have looked online for the meaning of parent::init(); . All I was able to find was that init() is to initialize some settings which want to be present every time the application runs. Can anyone please explain the meaning of parent::init() in exact sense, like significance of both the words? Thanks in advance.( I am sorry if its too simple! )

Mohit Garg
  • 315
  • 3
  • 13

2 Answers2

3

When we use parent::init(), we are just calling the parent method (in this case init()) inside a method of the current class.

About parent::

For example, let's say we have a class called MyClass. This class have a awesome method that runs alot of things:

class MyClass
{
    public function runStuffs()
    {
        // trigger events, configure external stuff, adding default values to properties.
    }
}

Now, after some time, we decided to create a new Class that extends from the first one. And we called MySecondClass:

class MySecondClass extends MyClass
{

}

It already have the method runStuffs(), but, for this second class, we need to do more things in this method, but maintaining what it have.

Sure we could rewrite the whole method and just copy and paste what we have in MyClass and add the new content. But this isn't elegant or even a good practice. what if later on We change the method in MyClass, you probably would like that MysecondClass have that changes too.

So, to solve that problem, we can call the parent method before write your new content:

class MySecondClass extends MyClass
{
    public function runStuffs()
    {
        parent::runStuffs();

        // do more things!
    }
}

Now MySecondClass->runStuffs() will always do what its parent do and, after that, more stuff.

About the init() method.

init() is a method used in almost all classes from Yii2 framework (since most of then extends from yii\base\Object at some point) and works just like the __constructor() method (native from PHP). But there is some differences, you can read more here.

Actually the init() method is called inside the __constructor(), and the framework encorage us to use init() instead of __construct() whenever is possible.

Now if both are pretty much the same thing, why do they create this method? There is an answer for that here. (take a look at qiang's answer, from the dev team):

One of the reasons for init() is about life cycles of an object (or a component to be exact).

With an init() method, it is possible to configure an object after it is instantiated while before fully initialized. For example, an application component could be configured using app config. If you override its init() method, you will be sure that the configuration is applied and you can safely to check if everything is ready. Similar thing happens 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 set up the life cycles of an application and may be overridden so that the customization only occurs at expected life cycles.

Conclusion

So, when you use a init() method and calls parent::init() you are just saying you want to add more things to that method without removing what it already was doing.

Community
  • 1
  • 1
Clyff
  • 3,826
  • 2
  • 14
  • 32
2

The parent::init(); Method is useful to execute a code before every controller and action,

With an init() method, it is possible to configure an object after it is instantiated while before fully initialized. For example, an application component could be configured using app config. If you override its init() method, you will be sure that the configuration is applied and you can safely to check if everything is ready. Similar thing happens to a widget and other configurable components.

In Yii, init() method means that an object is already fully configured and some additional initialization work should be done in this method.

For More Information check this link : https://stackoverflow.com/questions/27180059/execute-my-code-before-any-action-of-any-controller

Execute my code before any action of any controller

might be helpful to you.

Community
  • 1
  • 1
vijay nathji
  • 1,490
  • 11
  • 22