Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to convert enum to string in php

Do it your self without of magic ! ;-) We have PHP 8.1 now.

enum DataType: string {
    case ACCEPTED = "accepted";
    case WITH_ERRORS = "with errore";
    case DONE = "done";
    case FAILED ='failed';

    public function toString(): string {
      return match($this){
        self::ACCEPTED => 'Accepted',
        self::WITH_ERRORS => 'With Errors',
        self::DONE  => 'Done',
        self::FAILED  => 'Failed',
        default => '',
      };
    }
}

echo DataType::DONE->toString();
 
PREVIOUS NEXT
Tagged: #convert #enum #string #php
ADD COMMENT
Topic
Name
7+4 =