Enums in PHP
Our enum type ServerState
has an underlying int
type.
The possible values for ServerState
are defined as constants. This approach automatically provides successive constant values 0, 1, 2, and so on.
By providing a toString
method, values of ServerState
can be printed out or converted to strings.
The transition
function emulates a state transition for a server. It takes the existing state and returns a new state.
In the main
function, we demonstrate state transitions starting from StateIdle
.
If we run the program using PHP, it will output connected
followed by idle
.