문제

If i write this code in PHP with Stricts Standarts, i've an non-compliance:

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

What is the principle (like SOLId, ...) failure here ?

Note: if i'm right, this code respect Liskov principle.

도움이 되었습니까?

해결책

Simply speaking, Liskov substitution principle just states that inheritance should be logical part of a class it inherits from to satisfy is-a relationship.

As for strict standards, PHP expects an overridden method to have exactly the same signature as its parent. In your case its a just a different type-hint. Instead you should type-hint an interface that both classes do implement.

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

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

다른 팁

If it is Reader::read(Readable $readable), then the method in child class must use Readable too.

class AtomReader extends Reader { 
  public function read(Readable $readable){} 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top