Skip to content

Commit 36b1977

Browse files
authored
Fix missing compile error when declaring hooked props on readonly classes (phpGH-15439)
Fixes phpGH-15419
1 parent 770616b commit 36b1977

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
(zeriyoshi)
88
. Fixed bug GH-15438 (Hooks on constructor promoted properties without
99
visibility are ignored). (ilutov)
10+
. Fixed bug GH-15419 (Missing readonly+hook incompatibility check for readonly
11+
classes). (ilutov)
1012

1113
15 Aug 2024, PHP 8.4.0beta3
1214

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-15419: Readonly classes may not declare properties with hooks
3+
--FILE--
4+
<?php
5+
6+
readonly class C {
7+
public int $prop { set => $value; }
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Hooked properties cannot be readonly in %s on line %d
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-15419: Readonly classes may not declare promoted properties with hooks
3+
--FILE--
4+
<?php
5+
6+
readonly class C {
7+
public function __construct(
8+
public int $prop { set => $value; },
9+
) {}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Hooked properties cannot be readonly in %s on line %d

Zend/zend_compile.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -8385,6 +8385,10 @@ static void zend_compile_property_hooks(
83858385
{
83868386
zend_class_entry *ce = CG(active_class_entry);
83878387

8388+
if (prop_info->flags & ZEND_ACC_READONLY) {
8389+
zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8390+
}
8391+
83888392
if (hooks->children == 0) {
83898393
zend_error_noreturn(E_COMPILE_ERROR, "Property hook list cannot be empty");
83908394
}
@@ -8608,11 +8612,6 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
86088612
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
86098613
}
86108614

8611-
if (hooks_ast && (flags & ZEND_ACC_READONLY)) {
8612-
zend_error_noreturn(E_COMPILE_ERROR,
8613-
"Hooked properties cannot be readonly");
8614-
}
8615-
86168615
if (type_ast) {
86178616
type = zend_compile_typename(type_ast);
86188617

0 commit comments

Comments
 (0)