Bug #81293 [Csd->Nab]: 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
Updated by: requinix@php.net
Reported by: raincomplain at outlook dot com
Summary: Abstract method cases() in trait imoses contract
upon the exhibiting enum
-Status: Closed
+Status: Not a bug
Type: Bug
Package: Class/Object related
PHP Version: 8.1.0beta1
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2021-07-26 03:20:48] raincomplain at outlook dot com
You are right my mistake. The behavior is actually consistent with how abstract methods in general
work. I mistakenly presumed that this may lead to futue bugs especially when using trait aliases,
but it's not. Thank you for your feedback Damian.
------------------------------------------------------------------------
[2021-07-26 02:39:15] requinix@php.net
I don't see how this "is already implemented" error would be consistent with how
abstract trait methods work. You defined a method with a signature in the trait, you imported it
into a "class" with an incompatible signature, and PHP told you they didn't agree.
That's normal. The fact that PHP gave you the implementation instead of you having to write it
out yourself doesn't make a difference.
------------------------------------------------------------------------
[2021-07-26 01:15:42] raincomplain at outlook dot com
I know it's allowed which makes no sense. I made the return type int on purpose to trigger the
error.
------------------------------------------------------------------------
[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)