Skip to content

Commit 2d1d16c

Browse files
committed
Add test + improve error message for static properties of readonly classes
1 parent 96f5317 commit 2d1d16c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Declaring static property for a readonly class is forbidden
3+
--FILE--
4+
<?php
5+
6+
readonly class Foo
7+
{
8+
public static int $bar;
9+
}
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Readonly class Foo cannot declare static properties in %s on line %d

Zend/zend_compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7223,6 +7223,12 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
72237223
zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
72247224
}
72257225

7226+
if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS) && (flags & ZEND_ACC_STATIC)) {
7227+
zend_error_noreturn(E_COMPILE_ERROR, "Readonly class %s cannot declare static properties",
7228+
ZSTR_VAL(ce->name)
7229+
);
7230+
}
7231+
72267232
for (i = 0; i < children; ++i) {
72277233
zend_property_info *info;
72287234
zend_ast *prop_ast = list->child[i];

Zend/zend_compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ typedef struct _zend_oparray_context {
240240
/* or IS_CONSTANT_VISITED_MARK | | | */
241241
#define ZEND_CLASS_CONST_IS_CASE (1 << 6) /* | | | X */
242242
/* | | | */
243-
/* Class Flags (unused: 16,21,30,31) | | | */
243+
/* Class Flags (unused: 16,21,30,31) | | | */
244244
/* =========== | | | */
245245
/* | | | */
246246
/* Special class types | | | */

0 commit comments

Comments
 (0)