Bug #75590 [Com]: Fatal error when removing optional argument in subclass (BC break).

From: Date: Sat, 16 Mar 2019 05:48:24 +0000
Subject: Bug #75590 [Com]: Fatal error when removing optional argument in subclass (BC break).
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219997@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75590&edit=1 ID: 75590 Comment by: sshambir at gmail dot com Reported by: christoph at burschka dot de Summary: Fatal error when removing optional argument in subclass (BC break). Status: Not a bug Type: Bug Package: *General Issues Operating System: MacOS PHP Version: 7.2.0RC6 Block user comment: N Private report: N New Comment: Could you at least document this breaking change? Previous Comments: ------------------------------------------------------------------------ [2017-12-08 22:34:17] pollita@php.net The fact that PHP < 7.2 accepted this was an unintentional side-effect. The code you present doesn't follow valid inheritance contract. Consider: function foo(B $b) { $b->f(new A); } That code *should* always work, because B::f() declares a contract by which it accepts an instance of A as an argument, and therefore all children of B must adhere to that contract. However, the following will fail, even in 7.1 foo(new C); Because while C is a valid instance of B and will pass the type check, C::f() does not accept arguments and thus violates its parent's contract. ------------------------------------------------------------------------ [2017-11-29 17:08:53] christoph at burschka dot de Also note: If the interface declares the optional argument as well, then the result is a fatal error in both 7.1.11 and 7.2.0RC6. ------------------------------------------------------------------------ [2017-11-29 17:02:56] christoph at burschka dot de Description: ------------ In this inheritance chain, an interface defines a function without an argument, then an implementing class adds an optional argument, and a subclass defines the function with the original signature from the interface (no argument). This works in PHP 7.1.11, but triggers E_ERROR in PHP 7.2.0RC6. (Note: Without the interface, simply removing an optional argument in a subclass is E_WARNING on both 7.1.11 and 7.2.0RC6.) Test script: --------------- interface A { public function f(); } class B implements A { public function f(A $x = NULL) {} } class C extends B { public function f() {} } Expected result: ---------------- Same behavior in PHP 7.2.0RC6 as PHP 7.1.11, or at worst a non-fatal warning/deprecation. Actual result: -------------- No warning in 7.1.11, E_ERROR in 7.2.0RC6. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75590&edit=1

« previous php.bugs (#219997) next »