In modern versions of PHP (5.6 below), the following invalid program
error_reporting(E_ALL); class A { public function hello(X $one, Y $two) { } } class B extends A { public function hello() { } } interface X { } interface Y { } $b = new B;
PHP will refuse to run this and instead you will get the following error message
PHP Strict standards: Declaration of B::hello() should be compatible with A::hello(X $one, Y $two) in test.php on line 15 Strict standards: Declaration of B::hello() should be compatible with A::hello(X $one, Y $two) in test.php on line 15
This is a good thing in terms of rigor. However, if you try the same thing using the constructor function
class A { public function __construct(X $one, Y $two) { } } class B extends A { public function __construct() { } }
PHP has no problems and the program will start.
Does anyone know the history and / or technical context of why a strict standard does not apply to constructors?
source share