Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Casting An Enum Case

<?php

enum E: string {
    case A = "ab";
}

(string) E::A;

TIL that enumeration cases cannot be cast. They can’t be to a boolean, because they are an object.

They can’t be cast to their scalar value (int or string), even though it looks reasonable to do.

Of course, they can be cast to array (they are simply put inside an array), or to an object (nothing happens).

See Also

PHP Error Messages

PHP Features

Last updated: 10 September 2026