gpt4 book ai didi

PHP > 5.4 : overriding constructor with different signature

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:08:12 26 4
gpt4 key购买 nike

我们知道 PHP 不接受带有 different signature than the parent 的子方法.我认为构造函数也是如此:PHP 文档 states那个

This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.

但是,似乎继承的构造函数仍然在 PHP 版本 > 5.4 中可能有所不同。例如,以下代码不会触发任何警告或通知:

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();

根据文档,代码应该触发错误,因为构造函数签名不同。

在 PHP 5.6.4 上试过这个。与 other versions 效果相同.

那么,这是怎么回事?不管文档怎么说,不同的构造函数签名是否仍然合法?或者这是一个会在以后的版本中修复的错误?

最佳答案

根据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.

所以,这就是您没有收到 E_STRICT 级别错误的原因。也许它会触发不同层面的事情。

关于PHP > 5.4 : overriding constructor with different signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com