php.net |  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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: raincomplain at outlook dot com
New email:
PHP Version: OS:

 

 [2021-07-25 15:43 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-25 20:24 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-07-25 20:24 UTC] 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-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] requinix@php.net
-Status: Open +Status: Feedback
 [2021-07-26 02:39 UTC] 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 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] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC