Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A PHP library for State Machines
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

Finite-State Machine Library

A simple PHP implementation of a Finite-State Machine


Use Composer to install this library in your project

Create your composer.json file

      "require": {
          "thetwelvelabs/techne": "0.2.*@dev"

Download composer into your application root

  $ curl -s | php

Install your dependencies

  $ php composer.phar install


Let's use a light switch as a simple example.
A light switch as two states: on and off. The state of a light switch is transitioned from one to the other by flipping the switch. We'll assume that the initial state of the light switch is 'off'

Define your FSM

  $machine = new StateMachine\FiniteStateMachine();

Define the transitions

  $turnOff = new StateMachine\Transition('on', 'off');
  $turnOn = new StateMachine\Transition('off', 'on');

Add a guard to the turnOn transition

  // flipping the switch on requires electricity
  $hasElectricity = true;
  $turnOn->before(function() use ($hasElectricity) {
      return $hasElectricity ? true : false;

Define the events

  $machine->addEvent('flip', array($turnOn, $turnOff));

Transition from off to on

  echo $machine->getCurrentState();
  // prints 'on'  

Transition back to off

  echo $machine->getCurrentState();
  // prints 'off'  
Something went wrong with that request. Please try again.