Skip to content

Commit 35dcc94

Browse files
committed
Fix #[Override] on traits overriding a parent method without a matching interface
1 parent 2351df3 commit 35dcc94

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
#[Override] attribute in trait does not check for parent class implementations
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function foo(): void {}
8+
}
9+
10+
interface I {
11+
public function foo(): void;
12+
}
13+
14+
trait T {
15+
#[\Override]
16+
public function foo(): void {
17+
echo 'foo';
18+
}
19+
}
20+
21+
// Works fine
22+
class B implements I {
23+
use T;
24+
}
25+
26+
// Works fine ("copied and pasted into the target class")
27+
class C extends A {
28+
#[\Override]
29+
public function foo(): void {
30+
echo 'foo';
31+
}
32+
}
33+
34+
// Does not work
35+
class D extends A {
36+
use T;
37+
}
38+
echo "Done";
39+
40+
?>
41+
--EXPECT--
42+
Done

Zend/zend_inheritance.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,7 @@ static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_
19461946
{
19471947
zend_function *existing_fn = NULL;
19481948
zend_function *new_fn;
1949+
bool inherited = 0;
19491950

19501951
if ((existing_fn = zend_hash_find_ptr(&ce->function_table, key)) != NULL) {
19511952
/* if it is the same function with the same visibility and has not been assigned a class scope yet, regardless
@@ -1985,6 +1986,8 @@ static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_
19851986
do_inheritance_check_on_method(
19861987
fn, fixup_trait_scope(fn, ce), existing_fn, fixup_trait_scope(existing_fn, ce),
19871988
ce, NULL, /* check_visibility */ 1);
1989+
1990+
inherited = 1;
19881991
}
19891992
}
19901993

@@ -2004,6 +2007,12 @@ static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_
20042007
function_add_ref(new_fn);
20052008
fn = zend_hash_update_ptr(&ce->function_table, key, new_fn);
20062009
zend_add_magic_method(ce, fn, key);
2010+
2011+
if (inherited && new_fn->common.fn_flags & ZEND_ACC_OVERRIDE) {
2012+
do_inheritance_check_on_method(
2013+
new_fn, new_fn->common.scope, existing_fn, existing_fn->common.scope,
2014+
new_fn->common.scope, NULL, /* check_visibility */ 1);
2015+
}
20072016
}
20082017
/* }}} */
20092018

0 commit comments

Comments
 (0)