php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Attribute parameters cannot contain BackedEnum values

Open michnovka opened this issue 4 years ago • 18 comments

Description

The following code:

<?php

enum Status: string
{
    case DRAFT = 'draft';
    case PUBLISHED = 'published';
    case ARCHIVED = 'archived';
}

#[Attribute]
class ListensTo
{
    public string $event;

    public function __construct(string $event)
    {

        $this->event = $event;
    }
}

#[ListensTo(Status::DRAFT->value)]
class TestClass {}

Resulted in this output:

PHP Fatal error:  Constant expression contains invalid operations on line 23

But I expected it would work fine

PHP Version

PHP 8.1.4

Operating System

Ubuntu 20.04

michnovka avatar Apr 11 '22 18:04 michnovka

->value isn't a constant expression: it requires a class instance to get the property from.

Why not use the enum properly? https://3v4l.org/BCqrS

damianwadley avatar Apr 11 '22 19:04 damianwadley

@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 Status::DRAFT, I need to change it on more places.

michnovka avatar Apr 11 '22 19:04 michnovka

There's two ways to solve this:

  1. Allow -> in constant expressions
  2. Allow enums (or objects) as array keys (https://wiki.php.net/rfc/object_keys_in_arrays)

Solution 1 didn't make sense before enums and before allowing new in constant expressions but I guess at this point it would be somewhat more sensible. @nikic WDYT?

iluuu1994 avatar Apr 11 '22 19:04 iluuu1994

Yeah, I think supporting -> in constant expressions makes sense.

nikic avatar Apr 11 '22 19:04 nikic

Can we target this as a bug for 8.1?

michnovka avatar Apr 11 '22 19:04 michnovka

@michnovka This is not a bug but a limitation. -> were never supported for constant expressions. New features only land in new minor/major versions of PHP.

iluuu1994 avatar Apr 11 '22 19:04 iluuu1994

Alright. Do we need some proper vote on this? Or is my job done here and you guys will now take over? :)

michnovka avatar Apr 11 '22 20:04 michnovka

@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.

bwoebi avatar Apr 12 '22 11:04 bwoebi

Even if it were just for enums, the Enum::CASE->value is also always a constant, so we are not allowing some non-constant expressions this way. Is there any other place where -> could be used in constant expressions? I don think so

michnovka avatar Apr 12 '22 11:04 michnovka

@michnovka (new Foo)->bar

@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 avatar Apr 12 '22 11:04 iluuu1994

@iluuu1994 I dont know how Enum::CASE is handled internally in constant expressions now. Is it instantiated? If not, I would very much like to fix it just for Enums in this version and make the big change of allowing all -> in major. After all, I just need to access a "static property" of BackedEnum. It feels like a strong limitation that should not be mixed with assigning (new Foo)->bar to constant expressions

What I mean to say is that the syntax might as well have been Enum::CASE::value but you went with Enum::CASE->value. value is static property tho.

michnovka avatar Apr 12 '22 11:04 michnovka

@michnovka It's not easier if we allow -> just for enums, the implementation is the same.

What I mean to say is that the syntax might as well have been Enum::CASE::value

-> is used to access instance properties, that's what name is.

iluuu1994 avatar Apr 12 '22 11:04 iluuu1994

Then how about to allow access to value statically as well via Enum::CASE::value ?

michnovka avatar Apr 12 '22 11:04 michnovka

@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.

iluuu1994 avatar Apr 12 '22 11:04 iluuu1994

cc @Crell

cmb69 avatar Apr 12 '22 13:04 cmb69

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. (new Foo)->value is very close to what's actually going on anyway.

So yeah, my vote would be for allowing static -> de-references in constant expressions generally, as of 8.2. That... probably requires an RFC.

Crell avatar Apr 12 '22 15:04 Crell

I created a PoC for this here. I'll start the RFC process soon.

iluuu1994 avatar May 24 '22 22:05 iluuu1994

Given there's an RFC for this now I'm closing this issue. Thanks!

iluuu1994 avatar Jun 23 '22 17:06 iluuu1994