Skip to content

Commit bbfa507

Browse files
committed
Forbid default values after all
I changed my mind...
1 parent 94bdfeb commit bbfa507

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Zend/tests/readonly_props/readonly_with_default.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ try {
1515
}
1616

1717
?>
18-
--EXPECT--
19-
Cannot modify readonly property Test::$prop
18+
--EXPECTF--
19+
Fatal error: Readonly property Test::$prop cannot have default value in %s on line %d

Zend/tests/readonly_props/serialization.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Serialization of readonly properties
44
<?php
55

66
class Test {
7-
public readonly int $prop = 1;
7+
public function __construct(
8+
public readonly int $prop = 1,
9+
) {}
810
}
911

1012
var_dump($s = serialize(new Test));

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7361,6 +7361,11 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, z
73617361
zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
73627362
ZSTR_VAL(ce->name), ZSTR_VAL(name));
73637363
}
7364+
if (!Z_ISUNDEF(value_zv)) {
7365+
zend_error_noreturn(E_COMPILE_ERROR,
7366+
"Readonly property %s::$%s cannot have default value",
7367+
ZSTR_VAL(ce->name), ZSTR_VAL(name));
7368+
}
73647369
if (flags & ZEND_ACC_STATIC) {
73657370
zend_error_noreturn(E_COMPILE_ERROR,
73667371
"Static property %s::$%s cannot be readonly",

0 commit comments

Comments
 (0)