Skip to content

Commit f2708dd

Browse files
committed
Don't evaluate dynamic constants during trait compatibilty check
1 parent a929893 commit f2708dd

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Compatibility of trait properties with new default
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
// Should not be called.
8+
public function __construct() {
9+
echo "Constructor\n";
10+
}
11+
}
12+
13+
trait T1 {
14+
public $prop = new A;
15+
}
16+
trait T2 {
17+
public $prop = new A;
18+
}
19+
20+
class B {
21+
use T1, T2;
22+
}
23+
24+
?>
25+
--EXPECTF--
26+
Fatal error: T1 and T2 define the same property ($prop) in the composition of B. However, the definition differs and is considered incompatible. Class was composed in %s on line %d

Zend/zend_inheritance.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,13 +2062,17 @@ static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_ent
20622062
op2 = &traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
20632063
}
20642064

2065-
/* if any of the values is a constant, we try to resolve it */
2066-
if (UNEXPECTED(Z_TYPE_P(op1) == IS_CONSTANT_AST)) {
2065+
/* If any of the values is a constant, we try to resolve it. Don't resolve
2066+
* "dynamic" ASTs, which may have side-effects. For these we assume
2067+
* incompatibility. */
2068+
if (UNEXPECTED(Z_TYPE_P(op1) == IS_CONSTANT_AST)
2069+
&& !(GC_FLAGS(Z_AST_P(op1)) & IS_AST_DYNAMIC)) {
20672070
ZVAL_COPY_OR_DUP(&op1_tmp, op1);
20682071
zval_update_constant_ex(&op1_tmp, ce);
20692072
op1 = &op1_tmp;
20702073
}
2071-
if (UNEXPECTED(Z_TYPE_P(op2) == IS_CONSTANT_AST)) {
2074+
if (UNEXPECTED(Z_TYPE_P(op2) == IS_CONSTANT_AST)
2075+
&& !(GC_FLAGS(Z_AST_P(op2)) & IS_AST_DYNAMIC)) {
20722076
ZVAL_COPY_OR_DUP(&op2_tmp, op2);
20732077
zval_update_constant_ex(&op2_tmp, ce);
20742078
op2 = &op2_tmp;

0 commit comments

Comments
 (0)