The parent constructor was not called: the object is in an invalid state¶
Description¶
When extending certain PHP native classes, the child class must call the parent constructor to finish the instantiation of the object.
The problem is not detected at instantiation time, but later, as soon as any of the parent resources are used, but found to be in invalid state.
PHP does not automatically call the parent constructor when creating a child instance. It must be explicit in the code.
Some classes that needs such initialisation are: SplFileObject
, SplDirectory
, GlobIterator
. The list is not exhaustive.
Example¶
<?php
// PHP bug 8318
class Bug8318 extends \SplFileObject
{
public function __construct()
{
}
public function fpassthru(): int
{
return 0;
}
}
$cl = new Bug8318;
try {
$cl->fpassthru();
} catch (\Error $e) {
var_dump($e);
}
?>
Solutions¶
Call the parent constructor.
Use another parent class.