Skip to content

Commit 7b0993b

Browse files
committed
Fixed bug #63976 (Parent class incorrectly using child constant in class property)
1 parent 984561c commit 7b0993b

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ PHP NEWS
66
(Dmitry)
77
. Fixed bug #64370 (microtime(true) less than $_SERVER['REQUEST_TIME_FLOAT']).
88
(Anatol)
9+
. Fixed bug #63976 (Parent class incorrectly using child constant in class
10+
property). (Dmitry)
911
. Fixed bug #62343 (Show class_alias In get_declared_classes()) (Dmitry)
1012

1113
- PCRE:

Zend/tests/bug63976.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #63976 (Parent class incorrectly using child constant in class property)
3+
--FILE--
4+
<?php
5+
if (1) {
6+
class Foo {
7+
const TABLE = "foo";
8+
public $table = self::TABLE;
9+
}
10+
}
11+
if (1) {
12+
class Bar extends Foo {
13+
const TABLE = "bar";
14+
}
15+
}
16+
$bar = new Bar();
17+
var_dump($bar->table);
18+
?>
19+
--EXPECT--
20+
string(3) "foo"

Zend/zend_API.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,40 @@ ZEND_API void zend_merge_properties(zval *obj, HashTable *properties, int destro
10221022
}
10231023
/* }}} */
10241024

1025+
static int zval_update_class_constant(zval **pp, int is_static, int offset TSRMLS_DC) /* {{{ */
1026+
{
1027+
if ((Z_TYPE_PP(pp) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT ||
1028+
(Z_TYPE_PP(pp) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT_ARRAY) {
1029+
zend_class_entry **scope = EG(in_execution)?&EG(scope):&CG(active_class_entry);
1030+
1031+
if ((*scope)->parent) {
1032+
zend_class_entry *ce = *scope;
1033+
HashPosition pos;
1034+
zend_property_info *prop_info;
1035+
1036+
do {
1037+
for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
1038+
zend_hash_get_current_data_ex(&ce->properties_info, (void **) &prop_info, &pos) == SUCCESS;
1039+
zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
1040+
if (is_static == ((prop_info->flags & ZEND_ACC_STATIC) != 0) &&
1041+
offset == prop_info->offset) {
1042+
zend_class_entry *old_scope = *scope;
1043+
*scope = prop_info->ce;
1044+
int ret = zval_update_constant(pp, (void*)1 TSRMLS_CC);
1045+
*scope = old_scope;
1046+
return ret;
1047+
}
1048+
}
1049+
ce = ce->parent;
1050+
} while (ce);
1051+
1052+
}
1053+
return zval_update_constant(pp, (void*)1 TSRMLS_CC);
1054+
}
1055+
return 0;
1056+
}
1057+
/* }}} */
1058+
10251059
ZEND_API void zend_update_class_constants(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
10261060
{
10271061
if ((class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED) == 0 || (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count)) {
@@ -1034,7 +1068,7 @@ ZEND_API void zend_update_class_constants(zend_class_entry *class_type TSRMLS_DC
10341068

10351069
for (i = 0; i < class_type->default_properties_count; i++) {
10361070
if (class_type->default_properties_table[i]) {
1037-
zval_update_constant(&class_type->default_properties_table[i], (void**)1 TSRMLS_CC);
1071+
zval_update_class_constant(&class_type->default_properties_table[i], 0, i TSRMLS_CC);
10381072
}
10391073
}
10401074

@@ -1075,7 +1109,7 @@ ZEND_API void zend_update_class_constants(zend_class_entry *class_type TSRMLS_DC
10751109
}
10761110

10771111
for (i = 0; i < class_type->default_static_members_count; i++) {
1078-
zval_update_constant(&CE_STATIC_MEMBERS(class_type)[i], (void**)1 TSRMLS_CC);
1112+
zval_update_class_constant(&CE_STATIC_MEMBERS(class_type)[i], 1, i TSRMLS_CC);
10791113
}
10801114

10811115
*scope = old_scope;

0 commit comments

Comments
 (0)