|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81293 Abstract method cases() in trait imoses contract upon the exhibiting enum
Submitted: 2021-07-25 15:43 UTC Modified: 2021-07-26 03:22 UTC
From: raincomplain at outlook dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.1.0beta1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
25 - 23 = ?
Subscribe to this entry?

 [2021-07-25 15:43 UTC] raincomplain at outlook dot com
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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-25 20:24 UTC]
-Status: Open +Status: Feedback
 [2021-07-25 20:24 UTC]
> 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-26 01:15 UTC] raincomplain at outlook dot com
-Status: Feedback +Status: Open
 [2021-07-26 01:15 UTC] 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-26 02:39 UTC]
-Status: Open +Status: Feedback
 [2021-07-26 02:39 UTC]
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 03:20 UTC] raincomplain at outlook dot com
-Status: Feedback +Status: Closed
 [2021-07-26 03:20 UTC] 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 03:22 UTC]
-Status: Closed +Status: Not a bug
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jan 24 18:03:35 2022 UTC