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

An Enum With An Interface

<?php

interface i {}

enum A1 implements i {
    case A;
    case B;
}

enum A2 implements i {
    case C;
    case D;
}

function foo(i $e) {
    var_dump($e);
}

foo(A1::A);
foo(A2::C);

It is possible for an enumeration to implement an interface, even a marker interface. That way, it may group several enumerations together, to behave as one.

Obviously, conditions apply to do it, and, in the end, it is merely a short version of an union type. Readability may apply.

See Also

PHP Features

Last updated: 11 October 2026