为什么"严格标准"不适用于PHP构造函数?

Ala*_*orm 5 php oop

在现代版本的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没有问题,并将运行该程序.

有谁知道为什么严格标准不适用于构造函数的历史和/或技术背景?

Ben*_*old 4

来自PHP 手册

注意:如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用parent::__construct()。如果子类没有定义构造函数,那么它可以像普通的类方法一样从父类继承(如果它没有声明为私有)。

还:

与其他方法不同,当使用与父 __construct() 方法不同的参数覆盖 __construct() 时,PHP 不会生成E_STRICT级别错误消息。

如果您需要在所有扩展类上使用相同的构造函数签名,那么继承的有用性基本上就会被破坏。您能想象应用程序中的每个控制器都需要相同的构造函数签名吗?