为什么PHP允许“不兼容”构造函数? [英] Why does PHP allow "incompatible" constructors?

查看:185
本文介绍了为什么PHP允许“不兼容”构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是几个代码段:


  1. 覆盖 / p>

  1. Overriding constructor method has an extra parameter.

class Cat {
    function __construct() {}
}

class Lion extends Cat {
    function __construct($param) {}
}


  • 覆盖(常规)方法有一个额外的参数。

  • Overriding (regular) method has an extra parameter.

    class Cat {
        function doSomething() {}
    }
    
    class Lion extends Cat {
        function doSomething($param) {}
    }
    


  • 第一个会工作,第二个会调用 Declaration of Lion :: doSomething应该与Cat :: doSomething()的兼容。

    The first would work, while the second would throw Declaration of Lion::doSomething() should be compatible with that of Cat::doSomething().

    为什么对构造函数方法有特殊的态度?

    Why the special attitude towards constructor methods?

    推荐答案

    要了解为什么他们受到不同的对待,你必须了解 Liskov的替代原则状态

    To understand why they are treated differently, you have to understand Liskov's Substitution Principle, which states


    如果对于类型S的每个对象o1,存在类型T的对象o2,使得对于按照T定义的所有程序P,P的行为不变当o1代替o2时,则S是T的子类型。BarbaraLiskov,Data Abstraction and Hierarchy,SIGPLAN Notices,23,5(1988年5月)。

    If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T." - BarbaraLiskov, Data Abstraction and Hierarchy, SIGPLAN Notices, 23,5 (May, 1988).

    简而言之,这意味着使用您的 Lion Cat 应该能够可靠地调用 doSomething ,无论类是一个还是另一个。

    In a nutshell this means any class using your Lion or Cat should be able to reliably call doSomething on it, regardless of the class being one or the other. If you change the method signature, this is no longer guaranteed (you may widen it, but not narrow it though).

    public function doSomethingWithFeline(Cat $feline)
    {
        $feline->doSomething(42);
    }
    

    Lion延伸Cat ,你建立了一个is-a关系,意思是 doSomethingWithFeline 会接受 Lion Cat 。现在想象你在 Lion 中的 doSomething 中添加一个必需的参数。上面的代码会破解,因为它没有传递那个新的参数。因此,需要兼容的签名。

    Since Lion extends Cat, you established an is-a relationship, meaning doSomethingWithFeline will accept a Lion for a Cat. Now imagine you add a required argument to doSomething in Lion. The code above would break because it is not passing that new param. Hence, the need for compatible signatures.

    LSP不适用于构造函数,但,因为子类型可能有不同的依赖关系。例如,如果您有一个FileLogger和一个DBLogger,第一个的ctors(构造函数)将需要一个文件名,而后者将需要一个数据库适配器。因此,ctors是关于具体实现,而不是类之间的合同的一部分。

    LSP does not apply to constructors though, because subtypes might have different dependencies. For instance if you have a FileLogger and a DBLogger, the ctors (constructors) of the first would require a filename, while the latter would require a db adapter. As such, ctors are about concrete implementations and not part of the contract between classes.

    这篇关于为什么PHP允许“不兼容”构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆