diff --git a/NEWS b/NEWS index e8762fa2419c8..597673c7fe1e3 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,8 @@ PHP NEWS (zeriyoshi) . Fixed bug GH-15438 (Hooks on constructor promoted properties without visibility are ignored). (ilutov) + . Fixed bug GH-15419 (Missing readonly+hook incompatibility check for readonly + classes). (ilutov) 15 Aug 2024, PHP 8.4.0beta3 diff --git a/Zend/tests/property_hooks/gh15419_1.phpt b/Zend/tests/property_hooks/gh15419_1.phpt new file mode 100644 index 0000000000000..41a45154f1fde --- /dev/null +++ b/Zend/tests/property_hooks/gh15419_1.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-15419: Readonly classes may not declare properties with hooks +--FILE-- + $value; } +} + +?> +--EXPECTF-- +Fatal error: Hooked properties cannot be readonly in %s on line %d diff --git a/Zend/tests/property_hooks/gh15419_2.phpt b/Zend/tests/property_hooks/gh15419_2.phpt new file mode 100644 index 0000000000000..dfa6490fdc0cd --- /dev/null +++ b/Zend/tests/property_hooks/gh15419_2.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-15419: Readonly classes may not declare promoted properties with hooks +--FILE-- + $value; }, + ) {} +} + +?> +--EXPECTF-- +Fatal error: Hooked properties cannot be readonly in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 93c557df5a238..06a55ff761d6a 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -8385,6 +8385,10 @@ static void zend_compile_property_hooks( { zend_class_entry *ce = CG(active_class_entry); + if (prop_info->flags & ZEND_ACC_READONLY) { + zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly"); + } + if (hooks->children == 0) { zend_error_noreturn(E_COMPILE_ERROR, "Property hook list cannot be empty"); } @@ -8608,11 +8612,6 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; } - if (hooks_ast && (flags & ZEND_ACC_READONLY)) { - zend_error_noreturn(E_COMPILE_ERROR, - "Hooked properties cannot be readonly"); - } - if (type_ast) { type = zend_compile_typename(type_ast);