Error Identifier: constructor.missingParentCall
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class ParentClass
{
public function __construct()
{
// initialization logic
}
}
class ChildClass extends ParentClass
{
public function __construct(private string $name)
{
// missing parent::__construct() call
}
}
Why is it reported? #
This rule is part of phpstan-strict-rules.
A child class defines its own constructor but does not call the parent class’s constructor. When a parent class has a constructor with initialization logic, failing to call it from the child constructor can leave the object in an inconsistent or uninitialized state.
In the example above, ChildClass::__construct() does not call parent::__construct(), so the initialization logic in ParentClass is skipped.
How to fix it #
Call the parent constructor from the child constructor:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
public function __construct(private string $name)
{
- // missing parent::__construct() call
+ parent::__construct();
}
}
How to ignore this error #
You can use the identifier constructor.missingParentCall to ignore this error using a comment:
// @phpstan-ignore constructor.missingParentCall
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: constructor.missingParentCall
Rules that report this error #
- PHPStan\Rules\Classes\RequireParentConstructCallRule [1] phpstan/phpstan-strict-rules