在现代版本的PHP(下面的5.6)中,以下是无效的程序
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;
Run Code Online (Sandbox Code Playgroud)
PHP将拒绝运行此命令,而是提供如下错误消息
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
Run Code Online (Sandbox Code Playgroud)
从严格的角度来看,这是一件好事.但是,如果您使用构造函数尝试相同的事情
class A
{
public function __construct(X $one, Y $two)
{
}
}
class B extends A
{
public function __construct()
{
}
}
Run Code Online (Sandbox Code Playgroud)
PHP没有问题,并将运行该程序.
有谁知道为什么严格标准不适用于构造函数的历史和/或技术背景?