Skip to content

Commit 86a62eb

Browse files
committed
Fixed bug #79867
In line with usual rules, give untyped properties a null default value. Otherwise constructor promotion would give you a property declaration that cannot be achieved through any other means.
1 parent 6c8b94e commit 86a62eb

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
(Nikita)
1414
. Fixed bug #79852 (count(DOMNodeList) doesn't match
1515
count(IteratorIterator(DOMNodeList))). (Nikita)
16+
. Fixed bug #79867 (Promoted untyped properties should get null default
17+
value). (Nikita)
1618

1719
09 Jul 2020, PHP 8.0.0alpha2
1820

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #79867: Promoted untyped properties should get null default value
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function __construct(
8+
public $untyped = 1,
9+
public int $typed = 2,
10+
) {}
11+
}
12+
13+
class B extends A {
14+
public function __construct() {
15+
// Missing parent::__construct() call,
16+
// properties will not be initialized.
17+
}
18+
}
19+
20+
var_dump(new B);
21+
22+
?>
23+
--EXPECT--
24+
object(B)#1 (1) {
25+
["untyped"]=>
26+
NULL
27+
["typed"]=>
28+
uninitialized(int)
29+
}

Zend/zend_compile.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6197,16 +6197,21 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
61976197
ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
61986198
}
61996199

6200-
/* Always use uninitialized as the default. */
6201-
zval default_value;
6202-
ZVAL_UNDEF(&default_value);
6203-
62046200
/* Recompile the type, as it has different memory management requirements. */
62056201
zend_type type = ZEND_TYPE_INIT_NONE(0);
62066202
if (type_ast) {
62076203
type = zend_compile_typename(type_ast, /* force_allow_null */ 0, /* use_arena */ 1);
62086204
}
62096205

6206+
/* Don't give the property an explicit default value. For typed properties this means
6207+
* uninitialized, for untyped properties it means an implicit null default value. */
6208+
zval default_value;
6209+
if (ZEND_TYPE_IS_SET(type)) {
6210+
ZVAL_UNDEF(&default_value);
6211+
} else {
6212+
ZVAL_NULL(&default_value);
6213+
}
6214+
62106215
zend_string *doc_comment =
62116216
doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
62126217
zend_property_info *prop = zend_declare_typed_property(

0 commit comments

Comments
 (0)