-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute parameters cannot contain BackedEnum values #8344
Comments
Why not use the enum properly? https://3v4l.org/BCqrS |
@damianwadley I tried to make the example as minimal as possible. The reason why not use the enum directly is that I need it as an array key for my implementation <?php
enum Status: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
#[Attribute]
class ListensTo
{
public array $events;
public function __construct(array $events)
{
$this->events = $events;
}
}
#[ListensTo([Status::DRAFT->value => true])]
class TestClass {} This Attribute is from a 3rd party library and I cannot change it. The key values correspond to values of enum cases I have specified elsewhere in the project. So now I have to have "magic strings" like #[ListensTo(['draft' => true])] And when I change |
There's two ways to solve this:
Solution 1 didn't make sense before enums and before allowing |
Yeah, I think supporting |
Can we target this as a bug for 8.1? |
@michnovka This is not a bug but a limitation. |
Alright. Do we need some proper vote on this? Or is my job done here and you guys will now take over? :) |
@iluuu1994 There are instances where such limitations (especially when discovered in the version they were introduced in) these were fixed within that version. Given that implementing this is not breaking at all (I suppose), there is a reasonable case to just fix it. But I'll leave it at your discretion. |
Even if it were just for enums, the |
@michnovka @bwoebi I'd be more comfortable in a minor/major, especially if changes are bigger. What would be worse is releasing it in a patch, then realizing there's some issue and removing it again. |
@iluuu1994 I dont know how What I mean to say is that the syntax might as well have been |
@michnovka It's not easier if we allow
|
Then how about to allow access to value statically as well via
Enum::CASE::value ?
|
@michnovka Well, special casing enums here doesn't make sense IMO, especially not just to allow this edge case. And I don't think that would be easier either. I think it's fair for this to wait for PHP 8.2. |
cc @Crell |
I can't think of a good language argument to not allow En::Case->value (and I suppose also En::Case->name) in constant expressions. I could argue that it's a design flaw in an API, but that's a separate matter. If Nikita thinks it's safe them I'm fine with allowing it. I agree with others here that this sounds like an 8.2 feature. I also don't see a good reason to limit it to enums, when enums are really just sugar over objects to begin with. So yeah, my vote would be for allowing static -> de-references in constant expressions generally, as of 8.2. That... probably requires an RFC. |
I created a PoC for this here. I'll start the RFC process soon. |
Given there's an RFC for this now I'm closing this issue. Thanks! |
Description
The following code:
Resulted in this output:
But I expected it would work fine
PHP Version
PHP 8.1.4
Operating System
Ubuntu 20.04
The text was updated successfully, but these errors were encountered: