1.2.957. Parent First¶
When calling parent constructor, always put it first in the
__construct
method.
It ensures the parent is correctly build before the child start using values. This analysis doesn’t apply to Exceptions.
<?php
class father {
protected $name = null;
function __construct() {
$this->name = init();
}
}
class goodSon {
function __construct() {
// parent is build immediately,
parent::__construct();
echo "my name is ".$this->name;
}
}
class badSon {
function __construct() {
// This will fail.
echo "my name is ".$this->name;
// parent is build later,
parent::__construct();
}
}
?>
1.2.957.1. Suggestions¶
Use
parent::__construct
as the first call in the constructor.
1.2.957.2. Specs¶
Short name |
Classes/ParentFirst |
Rulesets |
|
Exakat since |
1.0.5 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Features |
parent |
Examples |
|
Available in |