Вопрос

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