我听说Liskov替换原则(LSP)是面向对象设计的基本原则.它是什么以及它的使用例子是什么?
oop liskov-substitution-principle definition design-principles solid-principles
我通常会尝试确保我的对象实例符合Liskov替换原则,但我一直想知道人们是否认为LSP也应该适用于构造函数?
我已经尝试使用谷歌搜索,但无论如何我都无法找到任何强烈的意见.
我应该注意到我的大部分编码都是在Ruby中,但我有时会发现我的子类构造函数与父类略有不同.它们使用相同的基本参数集,通常是额外的参数.有时这也会发生在其他类方法中.
在我的脑后,这总是感觉像LSP违规,但我想看看是否有其他人也有这种感觉.
在现代版本的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;
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
从严格的角度来看,这是一件好事.但是,如果您使用构造函数尝试相同的事情
class A
{
    public …