Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Zend/tests/enum/no-inherited-properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum disallows inherited instance properties
--FILE--
<?php

trait T {
public $bar;
}

enum Foo {
use T;
}

?>
--EXPECTF--
Fatal error: Enum "Foo" may not include instance properties in %s on line 7
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-name-property.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enums may not include instance properties in %s on line %d
4 changes: 2 additions & 2 deletions Zend/tests/enum/no-properties.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Enum disallows properties
Enum disallows instance properties
--FILE--
<?php

Expand All @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enums may not include instance properties in %s on line %d
12 changes: 0 additions & 12 deletions Zend/tests/enum/no-static-properties.phpt

This file was deleted.

2 changes: 1 addition & 1 deletion Zend/tests/enum/no-value-property.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo: int {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enums may not include instance properties in %s on line %d
32 changes: 32 additions & 0 deletions Zend/tests/enum/static-properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Enum allows static properties
--FILE--
<?php

trait Logger {
protected static bool $loggingEnabled;
}

enum Environment {
case DEV;
case STAGE;
case PROD;

use Logger;

protected static Environment $current;

public static function set(Environment $new): void {
self::$current = $new;
echo self::$current->name, "\n";
self::$loggingEnabled = self::$current !== self::PROD;
var_dump(self::$loggingEnabled);
}
}

Environment::set(Environment::DEV);

?>
--EXPECT--
DEV
bool(true)
2 changes: 1 addition & 1 deletion Zend/tests/enum/traits-no-properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ enum Suit {

?>
--EXPECTF--
Fatal error: Enum "Suit" may not include properties in %s on line %d
Fatal error: Enum "Suit" may not include instance properties in %s on line %d
4 changes: 2 additions & 2 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7159,8 +7159,8 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, z
zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include properties");
}

if (ce->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_COMPILE_ERROR, "Enums may not include properties");
if ((ce->ce_flags & ZEND_ACC_ENUM) && !(flags & ZEND_ACC_STATIC)) {
zend_error_noreturn(E_COMPILE_ERROR, "Enums may not include instance properties");
}

if (flags & ZEND_ACC_ABSTRACT) {
Expand Down
7 changes: 5 additions & 2 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ static void zend_verify_enum_properties(zend_class_entry *ce)
) {
continue;
}
if (property_info->flags & ZEND_ACC_STATIC) {
continue;
}
// FIXME: File/line number for traits?
zend_error_noreturn(E_COMPILE_ERROR, "Enum \"%s\" may not include properties",
zend_error_noreturn(E_COMPILE_ERROR, "Enum \"%s\" may not include instance properties",
ZSTR_VAL(ce->name));
} ZEND_HASH_FOREACH_END();
}
Expand Down Expand Up @@ -212,7 +215,7 @@ void zend_enum_add_interfaces(zend_class_entry *ce)

if (ce->enum_backing_type != IS_UNDEF) {
ce->interface_names[num_interfaces_before + 1].name = zend_string_copy(zend_ce_backed_enum->name);
ce->interface_names[num_interfaces_before + 1].lc_name = zend_string_init("backedenum", sizeof("backedenum") - 1, 0);
ce->interface_names[num_interfaces_before + 1].lc_name = zend_string_init("backedenum", sizeof("backedenum") - 1, 0);
}
}

Expand Down