Skip to content

Commit 7706069

Browse files
committed
Add separate static property through trait if parent already declares it
Closes GH-10935
1 parent 2ba3c99 commit 7706069

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Zend/tests/gh10935.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-1093: Add separate static property through trait if parent already declares it
3+
--FILE--
4+
<?php
5+
trait Foo {
6+
static $test;
7+
}
8+
9+
class A {
10+
use Foo;
11+
}
12+
13+
class B extends A {
14+
use Foo;
15+
}
16+
17+
A::$test = 'A';
18+
B::$test = 'B';
19+
var_dump(A::$test, B::$test);
20+
?>
21+
--EXPECT--
22+
string(1) "A"
23+
string(1) "B"

Zend/zend_inheritance.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,9 @@ static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_ent
23772377
ZSTR_VAL(prop_name),
23782378
ZSTR_VAL(ce->name));
23792379
}
2380-
continue;
2380+
if (!(flags & ZEND_ACC_STATIC)) {
2381+
continue;
2382+
}
23812383
}
23822384
}
23832385

ext/opcache/zend_persist.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,15 @@ void zend_update_parent_ce(zend_class_entry *ce)
10821082
end = parent->parent ? parent->parent->default_static_members_count : 0;
10831083
for (; i >= end; i--) {
10841084
zval *p = &ce->default_static_members_table[i];
1085-
ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
1085+
/* The static property may have been overridden by a trait
1086+
* during inheritance. In that case, the property default
1087+
* value is replaced by zend_declare_typed_property() at the
1088+
* property index of the parent property. Make sure we only
1089+
* point to the parent property value if the child value was
1090+
* already indirect. */
1091+
if (Z_TYPE_P(p) == IS_INDIRECT) {
1092+
ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
1093+
}
10861094
}
10871095

10881096
parent = parent->parent;

0 commit comments

Comments
 (0)