Re: How do I handle covariant parameters and not fall foul of LSP.

From: Date: Tue, 21 Feb 2017 22:48:17 +0000
Subject: Re: How do I handle covariant parameters and not fall foul of LSP.
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-325983@lists.php.net to get a copy of this message
On Sun, Feb 19, 2017 at 1:56 AM, Tony Marston <TonyMarston@hotmail.com> wrote: > "Richard Quadling" wrote in message >news:CAKUjMCXEXqy=VB8Jjjg8XKdU > sc5NsqQTeZcs54+V5e4GH1RYYw@mail.gmail.com... > >> >> <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

« previous php.general (#325983) next »