加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP> 5.4:覆盖具有不同签名的构造函数

发布时间:2020-12-13 17:48:43 所属栏目:PHP教程 来源:网络整理
导读:我们知道 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版本
我们知道 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级别的错误.也许它会触发不同程度的东西.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读