Skip to content

Attribute parameters cannot contain BackedEnum values #8344

Closed
@michnovka

Description

@michnovka

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions