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=jC1KjCSgwD6VOpOV=euO8g+Rp_f-f03SJdiWKXbwoXZMA@mail.gmail.com>
On Tue, Feb 14, 2017 at 2:20 AM, Richard Quadling <[email protected]>
 wrote:

> If the process method is doing storage related things I don't think is a
> good idea to move it to the Entity hierarchy.
>

I totally agree. I was just showing a way to avoid violating LSP.

On Tue, Feb 14, 2017 at 2:20 AM, Richard Quadling <[email protected]>
wrote:

> I still can't see why an extended BaseEntity is still not a BaseEntity,
> just a more specialised variant.
>

Because you're changing the contract in PersonEntity::processEntity() from
taking any BaseEntity to accepting only PersonEntity, you cannot substitute
any BaseEntity for it.

If anyone has some good examples (real world ideally) where LSP is sane AND
> includes inheritance (they seem to be where the conflict lies) then I may
> be able to learn why I'm doing it wrong.
>

A typical example is multiple cache backend implementations: CacheFile and
CacheMemcache. Swapping between the two requires no changes other than the
initial setup. The user of the cache shouldn't know or care which it has.

Cheers!
David
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.