How to use strict types with enums in PHP

1 Answer

0 votes
declare(strict_types=1);

enum Status: string {
    case OK = 'ok';
    case FAIL = 'fail';
}

function check(Status $s): void {
    echo $s->value;
}

check(Status::OK);     // OK
check("ok");           // TypeError




/*
run:

ERROR!
ok
Fatal error: Uncaught TypeError: check(): Argument #1 ($s) must be of type Status, string given

*/

 



answered Aug 9 by avibootz
...