|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-11-29 17:02 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 21:00:01 2025 UTC |
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.