We know that PHP doesn't accept child methods with a different signature than the parent. I thought that was the same with constructors: The PHP documentation states that
This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.
However, it appears that inherited constructors still can differ in PHP versions > 5.4. For example the following code does not trigger any warnings or notices:
class Something { }
class SomeOtherThing { }
class Foo
{
public function __construct(Something $foo)
{
}
public function yay()
{
echo 'yay';
}
}
class Bar extends Foo
{
public function __construct($foo, SomeOtherThing $bar = null)
{
}
}
$x = new Bar(new Something());
$x->yay();
According to the documentation, the code should trigger an error, as the contructor signatures are different.
Tried this on PHP 5.6.4. Same effect with other versions.
So, what's up with that? Are differing constructor signatures still legal, despite of what the documentation says? Or is this a bug which will be fixed in later versions?
According to the documentation
Unlike with other methods, PHP will not generate an E_STRICT level error message when __construct() is overridden with different parameters than the parent __construct() method has.
So, that is why you are not getting an error of level E_STRICT. Perhaps it will trigger something at a different level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With