Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some php classes have an empty init() method?

Tags:

php

yii2

For example in Yii2 framework The yii\filters\AccessControl class overrides an init() function from its parent class yii\base\Object. This Object class in turn has a constructor method like this:

    Class Object implements Configurable {
        public function __construct($config = [])
        {
            if (!empty($config)) {
                Yii::configure($this, $config);
        }
        $this->init(); // calls the method defined below
        }
    }
// and the definition of this init function ... 
    public function init()
    {
    }

Now there is no apparent use of writing such an empty function unless one wants to use for initializing some properties he/she may need in future.

But then the __construct() method has exactly the same use ! I need to understand how this init() method is helpful.

like image 957
Ramesh Pareek Avatar asked Jan 11 '16 13:01

Ramesh Pareek


People also ask

What is init function in PHP?

Definition and Usage. The init / mysqli_init() function initializes MySQLi and returns an object to use with the mysqli_real_connect() function.

How do you initialize a class in PHP?

All you need to do to initialize an object in PHP is to take the object name and use "->" followed by the property name. You then set this equal to the property value either through single quotes, double quotes, or nothing (if the value is a number, you have this option).

Is INIT called only once?

Unlike the main() function that can only be declared once, the init() function can be declared multiple times throughout a package.

Does every class need a constructor PHP?

You are not required to define a constructor in your class, but if you wish to pass any parameters on object construction then you need one.


2 Answers

The reason is simple: they're expecting you to extend the class

So you'd do something like this

class Bob extends Object {
    public function init() {
        $this->setup_something();
    }
}

So because Bob extends Object the method from Bob is the one that gets called. If you don't need to initialize something just skip the defintion and the base class will call the empty method.

This keeps you from having to do something potentially messier: overwrite the constructor

class Bob extends Object {
    public function __construct($config = []) {
         $this->setup_something();
    }
}

Now, this is messy because there's something critical a LOT of people miss (and I intentionally left out for this example): the parent constructor will not be called (you could do that by having parent::__construct($config)). So by having a separate init() you avoid having that mess entirely. There's an explicit method to do your own setup.

like image 108
Machavity Avatar answered Sep 30 '22 20:09

Machavity


It has been discussed at length on the Yii forums. Here's the Yii project lead:

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.

I agree with you that the method naming is very important. Here in Yii, init() method means that an object is already fully configured and some additional initialization work should be done in this method.

and:

Every function should exist for a reason. In the Yii case, the init() method is mainly offered to allow customization at certain life cycle of a component. In your calendar application, you may or may not need init(). If you intend to release it for public use, I would suggest you be more conservative at allowing customization. That is, do not define init() unless you have strong reason for it. Once you provide a protected or public method, it means you need to maintain it in future releases.

like image 40
ceejayoz Avatar answered Sep 30 '22 19:09

ceejayoz