Req #71825 [NEW]: PHP does not allow weaker parameter signature in child interface/class.
From: andreas at dqxtech dot net
Operating system: Linux
PHP version: 7.0.4
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:PHP does not allow weaker parameter signature in child interface/class.
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()"
--
Edit bug report at https://bugs.php.net/bug.php?id=71825&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71825&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71825&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71825&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71825&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71825&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71825&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71825&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71825&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71825&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71825&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71825&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71825&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71825&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71825&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71825&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71825&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71825&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71825&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71825&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71825&r=mysqlcfg
Thread (5 messages)
- andreas at dqxtech dot net