'Yii2 ActiveRecord recommends to call parent constructor after own logic. Isn't this strange?
I'm new to Yii2 and returning to PHP dev after a very long time. I have an extensive background in Java development. I stumbled onto this recommendation in the docs for ActiveRecord:
__construct()public methodDefined in: yii\base\BaseObject::__construct()
Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config.- Call init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$confighere.- call the parent implementation at the end of the constructor.
My question is about the last sentence:
call the parent implementation at the end of the constructor.
As a Java dev, this advice seems very weird to me. In Java, not only is it recommended to call the parent constructor as the very first call from your overridden constructor, this is even enforced and it's actually impossible to do it any other way. Either your parent constructor is called first implicitly, or, if you make an explicit call, it MUST be the very first statement in your method. This is enforced by the compiler.
Theoretically, this makes a lot of sense to me. Because as long as you did not call the parent constructor, your parent class did not have the chance to initialize, so any code you write in the constructor before calling the parent constructor would be working with a half-initialized object.
Looking at some SO answers I found, they seem to be going against the advice in the official docs and call the parent::__construct before their own custom logic, as I would expect it. For example, the accepted answer in the question How can I create a constructor in a Yii2 model shows an example where they call the parent first:
function __construct()
{
parent::__construct();
...
}
Another answer in that same question that does follow the official docs' advice scores 0 points:
public function __construct($config = []) {
// your init code here
// ...
parent::__construct();
}
Looking at the article about calling the parent constructor on phptutorial.net, they show an example where they call the parent first, as I would expect it:
class SavingAccount extends BankAccount
{
private $interestRate;
public function __construct($balance, $interestRate)
{
parent::__construct($balance);
$this->interestRate = $interestRate;
}
// ...
}
As I said my PHP is quitte rusty and I am a Yii2 n00b, so I was hoping to get some clarification here about this quitte fundamental thing that is still not clear to me after reading docs, tutorials and SO posts for a few hours now. This investigation was triggered by seeing the constructor being called both first and last very inconsistently in the codebase that I inherited and am currently working on.
In PHP, is there an official recommendation for when to call the parent constructor?
Is the advice to call the parent constructor last also in other PHP frameworks?
Hoping not to incite opinion-based discussion, so please quote official references, point to official docs of other projects etc.
Solution 1:[1]
@michal-hyn?ica correctly stated the reason for your ambiguity(Worthy of votUp).
But since you mentioned that the official reference, on the same page (BaseObject), this issue is explicitly stated: line
- In order to ensure the above life cycles, if a child class of BaseObject needs to override the constructor,
- ....
- of the constructor, and the parent implementation should be called at the end of the constructor.
According to the description of this link (similar to your question):
init () is called which further calls bootstrap () to run bootstrapping components.
As a result, this is done to ensure configuration.
The same process is done in the controller class and the main module.
Of course, there are many examples like this on the php site (depending on the need)
Also read lifecycles and entry script in yii. components and structure-applications. Good luck
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
