Re: [PHP] How do I handle covariant parameters and not fall foul of LSP.
[email protected] (David Harkness)
| Newsgroups | php.general |
|---|---|
| Message-ID | <CA+MN=jAFo+ZXEu5BJ62FCEJDfw8v6--nvf_-1qn9tDOCw4wHfw@mail.gmail.com> |
On Sun, Feb 19, 2017 at 1:56 AM, Tony Marston <[email protected]> wrote: > "Richard Quadling" wrote in message news:CAKUjMCXEXqy=VB8Jjjg8XKdU > [email protected]... > >> >> <snip> > >> >> That's fine for concrete classes, but abstract classes which cannot be >> instantiated, therefore cannot be swapped, cannot fall foul of LSP. It is >> the abstract part that, in my mind, should be making the difference. >> > > That is correct. LSP does not come into play when you inherit from an > abstract class, only when you inherit from one concrete class to create a > different concrete class. The author of LSP should have made this clear. I believe LSP should apply to all class hierarchies. If you have a List interface and supply an AbstractList partial implementation, all classes—concrete or not—that implement List or extend AbstractList should be substitutable for all others. LinkedList, ArrayList, SubList, etc. What Richard is encountering is a breakdown due to PHP not having support for generics. PersonRepository is a subclass of Entity and breaks LSP because we'd rather have Repository<Person>. So he's not really violating LSP in spirit, only in practice because PHP. Cheers! David