Skip to content

Commit af61092

Browse files
committed
Forbid default values after all
I changed my mind...
1 parent b7d6e64 commit af61092

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
@@ -7352,6 +7352,11 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, z
73527352
zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
73537353
ZSTR_VAL(ce->name), ZSTR_VAL(name));
73547354
}
7355+
if (!Z_ISUNDEF(value_zv)) {
7356+
zend_error_noreturn(E_COMPILE_ERROR,
7357+
"Readonly property %s::$%s cannot have default value",
7358+
ZSTR_VAL(ce->name), ZSTR_VAL(name));
7359+
}
73557360
if (flags & ZEND_ACC_STATIC) {
73567361
zend_error_noreturn(E_COMPILE_ERROR,
73577362
"Static property %s::$%s cannot be readonly",

0 commit comments

Comments
 (0)