PHPCompatibility Code
PHPCompatibility.FunctionNameRestrictions.RemovedPHP4StyleConstructors.Found
Recommendation
If you use PHPStorm, you can automatically fix this issue. Follow the Auto-Fix Inspection in PHPStorm guide.
If you don't use PHPStorm, you can follow the guide below to make those changes manually.
Before
class A extends B
{
function A()
{
$this->B();
}
}
class B
{
function B()
{
}
}
$var = new A();
After
class A extends B
{
function __construct()
{
B::__construct();
}
}
class B
{
function __construct()
{
}
}
$var = new A();
Explanation
Do not merely use parent::__construct
, as that can overlook an edge case that was only possible with the PHP 4 style constructors. Consider the following code:
class A extends B
{
function A()
{
$this->C();
}
}
class B extends C
{
function B()
{
throw new Exception('This should never be called');
}
}
class C
{
function C()
{
}
}
$var = new A();
If we were to replace $this->C()
with merely parent::__construct
, then we'd be executing the wrong constructor and alter the original logic. This is why we need to call the specific class' constructor using A::__construct
. PHPStorm's replacement is therefore the correct one.
Migration Services
Increase security and cut risk by simplifying PHP application upgrades and migrations.
Get Long-Term Support With ZendPHP
Stay secure and compliant while migrating on your schedule with Zend PHP LTS.