0

如果我在 PHP 中使用 Stricts Standarts 编写此代码,则不符合要求:

class Readable {};
class Atom extends Readable {};

class Reader { 
  public function read(Readable $readable){} 
}

class AtomReader extends Reader { 
  public function read(Atom $readable){} 
}

PHP Strict standards:  Declaration of AtomReader::read() should be compatible with Reader::read(Readable $readable) in php shell code on line 2

这里失败的原理(如SOLId,...)是什么?

注意:如果我是对的,此代码尊重 Liskov 原则

4

2 回答 2

0

如果是Reader::read(Readable $readable),则子类中的方法Readable也必须使用。

class AtomReader extends Reader { 
  public function read(Readable $readable){} 
}
于 2014-03-27T09:13:09.630 回答
0

简单地说,里氏替换原则只是指出继承应该是它所继承的类的逻辑部分以满足is-a关系。

至于严格的标准,PHP 期望一个被覆盖的方法与它的父方法具有完全相同的签名。在您的情况下,它只是一个不同的type-hint. 相反,您应该键入提示两个类都实现的接口。

class Reader { 
   public function read(ReadableAtomInterface $readable){} 
}

class AtomReader extends Reader { 
   public function read(ReadableAtomInterface $readable){} 
}
于 2014-03-27T09:17:39.580 回答