php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71825 PHP does not allow weaker parameter signature in child interface/class.
Submitted: 2016-03-14 22:32 UTC Modified: 2017-01-29 07:34 UTC
Votes:5
Avg. Score:4.0 ± 1.1
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:4 (80.0%)
From: andreas at dqxtech dot net Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 7.0.4 OS: Linux
Private report: No CVE-ID: None
 [2016-03-14 22:32 UTC] andreas at dqxtech dot net
Description:
------------
The Liskov substitution principle requires that any child thingy can replace the parent thingy. It does not require the opposite.

A weaker signature should be ok with the LSP.
Unfortunately, in PHP, it is not.


Demo: https://3v4l.org/MX0Cb

See also http://stackoverflow.com/questions/13423494/why-is-overriding-method-parameters-a-violation-of-strict-standards-in-php#comment18345860_13423625
"if the parameters are contravariant then the types are respected. If PHP was pure OO then there would be no problem, since Array would be narrower than Object (assuming that no type in the declaration means any object)."

One can always dream..

And, just saying: Others are making arguments about overloading. I think this is really a different story and does not apply here. To overload, you will use a narrower signature, so that the version of the method is only applied in some cases, not all. On the other hand, the broader / weaker signature always includes all cases of the parent, so this version will always be used.

Test script:
---------------
interface I {
    
    function foo(J $j);
    
}

interface J extends I {
    
    function foo(I $i);
    
}

Expected result:
----------------
Compile just fine.

Actual result:
--------------
"Fatal error: Declaration of J::foo() must be compatible with that of I::foo()"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-14 22:54 UTC] andreas at dqxtech dot net
https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Contravariant_method_argument_type
 [2016-03-26 21:31 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2016-03-26 21:31 UTC] krakjoe@php.net
For this kind of change, an RFC is required.

Please see: https://wiki.php.net/rfc/howto
 [2016-03-26 21:42 UTC] krakjoe@php.net
-Status: Closed +Status: Suspended
 [2016-04-04 14:32 UTC] jpauli@php.net
Yes, Liskov "widening" has been so many times debated so far, with no consensus about it.
We'll need to reopen the discussion through a RFC for it
 [2017-01-29 07:34 UTC] krakjoe@php.net
-Assigned To: krakjoe +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 05:01:29 2024 UTC