Jake
Jake

Reputation: 185

PHP enums always throws a Parse error Unexpected T_STRING

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();

Upvotes: 1

Views: 1090

Answers (1)

Jake
Jake

Reputation: 185

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.

Upvotes: 1

Related Questions