PHP State machine