PHPackages                             szykra/state-machine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. szykra/state-machine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

szykra/state-machine
====================

Simple Finite State Machine implementation

0.1.0(10y ago)114MITPHP

Since Aug 20Pushed 10y ago1 watchersCompare

[ Source](https://github.com/skrajewski/state-machine)[ Packagist](https://packagist.org/packages/szykra/state-machine)[ RSS](/packages/szykra-state-machine/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Finite State Machine
====================

[](#finite-state-machine)

[Finite State Machine](https://en.wikipedia.org/wiki/Finite-state_machine) implementation in PHP

Installation
------------

[](#installation)

### Install via composer

[](#install-via-composer)

Add dependency to your `composer.json` file and run `composer update`.

```
"szykra/state-machine": "~0.1.0"
```

or just run

```
composer require szykra/state-machine
```

Usage
-----

[](#usage)

### Prepare your entity to be stateful

[](#prepare-your-entity-to-be-stateful)

Your entity should implements interface `StatefulInterface`. It requires two new methods

- `public function getState();`
- `public function setState($state);`

Of course you should add new property named e.g. *state*.

### Configure a *StateMachine* by adding *States* and *Transitions*

[](#configure-a-statemachine-by-adding-states-and-transitions)

To control your entity by *StateMachine* you have to define:

- States
- Transitions between States

#### State

[](#state)

State object simply represents a state. It has name and list of available transitions.

```
$stateDraft = new State('draft');
$statePublished = new State('published');
```

#### Transition

[](#transition)

Transition is like single action which someone performs on an object. It means transition from one state to another state.

```
$stateDraft->addTransition('publish', 'draft', 'published');

$transitionReject = new Transition('reject', 'published', 'rejected');
$statePublished->putTransition($transitionReject);
```

Each state has own transitions so available transitions depend on the current state of object.

#### StateMachine

[](#statemachine)

StateMachine is a controller used to change state of stateful object. First you should create a *StateMachine* object and set up it by adding a valid *States*.

```
$stateMachine = new StateMachine();

// api inconsistent, should called putState
$stateMachine->addState($stateDraft);
$stateMachine->addState($statePublished);
```

### Initialize a State Machine

[](#initialize-a-state-machine)

When your *StateMachine* is ready you could initialize it by **Stateful Object**.

```
$document = new Document();
$stateMachine->initialize($document);
```

Now you could change state of `$document` by `$stateMachine`. To check if transition can be performed use the `can($transition)` method, e.g. if current state of `$document` is *draft* the results will be as follows

```
$stateMachine->can('publish'); // true
$stateMachine->can('reject'); // false
```

To change state of object you should use `run($transition)` method. If transition not exists `TransitionNotFoundException` will be thrown.

```
echo $document->getState();    // draft
$stateMachine->run('publish');
echo $document->getState();    // published
```

> If you want to see a complete example please see *tests/* directory.

To do
-----

[](#to-do)

- Ability to setup callbacks before/after change states
- List all states
- List all transitions
- Add configurable loader to StateMachine
- Add configurable conditionals to transitions

License
-------

[](#license)

The MIT License. Copyright © 2015 by Szymon Krajewski

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3924d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a82bc574f08a6357c22ba7ab58687c0e6c61c793b6db362128f23e938237baac?d=identicon)[skrajewski](/maintainers/skrajewski)

---

Top Contributors

[![skrajewski](https://avatars.githubusercontent.com/u/4791560?v=4)](https://github.com/skrajewski "skrajewski (5 commits)")

---

Tags

fsmfinite-state machinestate-machine

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/szykra-state-machine/health.svg)

```
[![Health](https://phpackages.com/badges/szykra-state-machine/health.svg)](https://phpackages.com/packages/szykra-state-machine)
```

###  Alternatives

[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

188.5k](/packages/tarfin-labs-event-machine)[eftec/statemachineone

A state Machine library for business processes

1144.0k](/packages/eftec-statemachineone)[metabor/statemachine

Statemachine in PHP 5.3

103150.7k2](/packages/metabor-statemachine)[pwm/s-flow

A lightweight library for defining state machines

742.5k](/packages/pwm-s-flow)[shrink0r/workflux

Finite state machine for php.

375.6k1](/packages/shrink0r-workflux)[thetwelvelabs/techne

Finite State Machine implementation

412.4k](/packages/thetwelvelabs-techne)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
