Skip to content

Commit 2aac616

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Don't treat failed assignment as initialization
2 parents d2e989d + 398cfb9 commit 2aac616

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
A failed assignment should not be considered an initialization
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public int $prop;
8+
9+
public function __get($name) {
10+
echo "__get() called\n";
11+
return 0;
12+
}
13+
}
14+
15+
$test = new Test;
16+
try {
17+
$test->prop;
18+
} catch (Error $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
try {
22+
$test->prop = "foo";
23+
} catch (Error $e) {
24+
echo $e->getMessage(), "\n";
25+
}
26+
try {
27+
$test->prop;
28+
} catch (Error $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
?>
33+
--EXPECT--
34+
Typed property Test::$prop must not be accessed before initialization
35+
Cannot assign string to property Test::$prop of type int
36+
Typed property Test::$prop must not be accessed before initialization

Zend/zend_object_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
743743
}
744744
if (Z_PROP_FLAG_P(variable_ptr) == IS_PROP_UNINIT) {
745745
/* Writes to uninitialized typed properties bypass __set(). */
746-
Z_PROP_FLAG_P(variable_ptr) = 0;
747746
goto write_std_property;
748747
}
749748
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
@@ -798,6 +797,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
798797
goto exit;
799798
}
800799
value = &tmp;
800+
Z_PROP_FLAG_P(variable_ptr) = 0;
801801
goto found; /* might have been updated via e.g. __toString() */
802802
}
803803

0 commit comments

Comments
 (0)