Skip to content

Commit a5eb57c

Browse files
pmmaganikic
authored andcommitted
Allow overriding abstract methods
RFC: https://wiki.php.net/rfc/allow-abstract-function-override
1 parent 5dc43b4 commit a5eb57c

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ PHP NEWS
4545
loosely-equal value). (pmmaga)
4646
. Fixed bug #61970 (Restraining __construct() access level in subclass gives
4747
a fatal error). (pmmaga)
48+
. Fixed bug #63384 (Cannot override an abstract method with an abstract
49+
method). (pmmaga, wes)
4850

4951
- BCMath:
5052
. Fixed bug #46564 (bcmod truncates fractionals). (liborm85)

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ PHP 7.2 UPGRADE NOTES
101101
inherited method. This complies with contravariance of method argument types
102102
under the Liskov Substitution Principle.
103103
(https://wiki.php.net/rfc/parameter-no-type-variance)
104+
. It is now allowed to override an abstract method with another abstract
105+
method in a child class.
106+
(https://wiki.php.net/rfc/allow-abstract-function-override)
104107
. A trailing comma in group use statements is now allowed.
105108
(https://wiki.php.net/rfc/list-syntax-trailing-commas)
106109

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Allow abstract function override
3+
--FILE--
4+
<?php
5+
6+
abstract class A { abstract function bar($x); }
7+
abstract class B extends A { abstract function bar($x); }
8+
9+
echo "DONE";
10+
?>
11+
--EXPECT--
12+
DONE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Allow abstract function override
3+
--FILE--
4+
<?php
5+
6+
abstract class A { abstract function bar($x); }
7+
abstract class B extends A { abstract function bar($x, $y = 0); }
8+
9+
echo "DONE";
10+
?>
11+
--EXPECT--
12+
DONE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Allow abstract function override
3+
--FILE--
4+
<?php
5+
6+
abstract class A { abstract function bar($x, $y = 0); }
7+
abstract class B extends A { abstract function bar($x); }
8+
9+
echo "DONE";
10+
?>
11+
--EXPECTF--
12+
Fatal error: Declaration of B::bar($x) must be compatible with A::bar($x, $y = 0) in %s

Zend/zend_inheritance.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,16 +551,6 @@ static void do_inheritance_check_on_method(zend_function *child, zend_function *
551551
uint32_t child_flags;
552552
uint32_t parent_flags = parent->common.fn_flags;
553553

554-
if ((parent->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0
555-
&& parent->common.fn_flags & ZEND_ACC_ABSTRACT
556-
&& parent->common.scope != (child->common.prototype ? child->common.prototype->common.scope : child->common.scope)
557-
&& child->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_IMPLEMENTED_ABSTRACT)) {
558-
zend_error_noreturn(E_COMPILE_ERROR, "Can't inherit abstract function %s::%s() (previously declared abstract in %s)",
559-
ZSTR_VAL(parent->common.scope->name),
560-
ZSTR_VAL(child->common.function_name),
561-
child->common.prototype ? ZSTR_VAL(child->common.prototype->common.scope->name) : ZSTR_VAL(child->common.scope->name));
562-
}
563-
564554
if (UNEXPECTED(parent_flags & ZEND_ACC_FINAL)) {
565555
zend_error_noreturn(E_COMPILE_ERROR, "Cannot override final method %s::%s()", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name));
566556
}

0 commit comments

Comments
 (0)