1.2.963. 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.963.1. Connex PHP features¶
1.2.963.1.1. Suggestions¶
Use
parent::__construct
as the first call in the constructor.
1.2.963.1.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 |
Examples |
|
Available in |