Bug #81293 [Fbk->Opn]: Abstract method cases() in trait imoses contract upon the exhibiting enum
Edit report at https://bugs.php.net/bug.php?id=81293&edit=1
ID: 81293
User updated by: raincomplain at outlook dot com
Reported by: raincomplain at outlook dot com
Summary: Abstract method cases() in trait imoses contract
upon the exhibiting enum
-Status: Feedback
+Status: Open
Type: Bug
Package: Class/Object related
PHP Version: 8.1.0beta1
Block user comment: N
Private report: N
New Comment:
I know it's allowed which makes no sense. I made the return type int on purpose to trigger the
error.
Previous Comments:
------------------------------------------------------------------------
[2021-07-25 20:24:43] requinix@php.net
> I think we should clarify with a proper error message that abstract cases() is
> not allowed too.
But it is allowed.
Test wants cases() to return an int, Size's cases() returns an array. They're
incompatible. Change the trait's method to return array and it works.
------------------------------------------------------------------------
[2021-07-25 15:43:03] raincomplain at outlook dot com
Description:
------------
Both Pure Enums and Backed Enums implement an internal interface named UnitEnum. UnitEnum includes a
static method cases(). So manually defining cases() method on an Enum results in a fatal error as
expected(as clarified in enum RFC). However, it seems it's allowed to define cases() in a
trait, resulting in cases() being a requirment in the exhibiting enum with somewhat a confused error
which makes no sense!. I think we should clarify with a proper error message that abstract cases()
is not allowed too. Non-abstract cases() is totally fine since the enum implementation will take
precedance over trait so no error message is needed here.
Test script:
---------------
enum Size: int
{
use Test;
}
trait Test
{
abstract static private function cases(): int;
}
Expected result:
----------------
PHP Fatal error: Abstract Size::cases() is already implemented
Actual result:
--------------
PHP Fatal error: Declaration of Size::cases(): array must be compatible with Test::cases(): int in
/var/www/html/index.php on line 0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81293&edit=1
Thread (6 messages)