1

I am trying out the new PHP 8.1 enums. What appeared to be simple, drives me crazy.

I have an enum and want to get the back the status as a string back. Every time I run the script, I get the following parse error:

ParseError : syntax error, unexpected 'Status' (T_STRING)

I read a lot of examples and all work the same. I really don't get the problem here.

Example Code:

<?php

enum Status : int
{
case Draft = 1;
case Published = 2;
case Archived = 3;

public function getStatus(): string
{
    return match($this)
    {
        self::DRAFT => 'draft',
        self::PUBLISHED => 'published',
        self::ARCHIVED => 'archived',
    };
}
}

echo Status::tryFrom(1)->getStatus();
Jake
  • 175
  • 6
  • 17

1 Answers1

1

Alright, make sure you're not only changing your PHP language level in your IDE but also actually switching to PHP 8.1 on your system.

Jake
  • 175
  • 6
  • 17