PHPackages                             zinovyev/php-fsm - 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. zinovyev/php-fsm

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

zinovyev/php-fsm
================

PHP Finite-state Machine

v0.6.5(12y ago)97.2k1GPLv2PHPPHP &gt;=5.4

Since May 17Pushed 8y ago2 watchersCompare

[ Source](https://github.com/zinovyev/php-fsm)[ Packagist](https://packagist.org/packages/zinovyev/php-fsm)[ RSS](/packages/zinovyev-php-fsm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)DependenciesVersions (6)Used By (0)

PHP Finite-state Machine
========================

[](#php-finite-state-machine)

Finite-state machine allows you to create an object, containing different states and transitions between them, that can change its behaivour according to the current state.

Install:
--------

[](#install)

For installing the FSM machine use Composer. Simply add following to your composer.json file:

```
    "require": {
        "zinovyev/php-fsm": "0.6.5"
    }
```

Creating a State machine steps:
-------------------------------

[](#creating-a-state-machine-steps)

- Create State classes and define states:

```
class StateA extends State
{
    public function foo($name)
    {
        printf("Hello, %s!", $name);
    }
}
```

- Create a new FSM\\Client instance (StateMachine).
- Bind your States to the Client instance, apply transactions and set initial state:

```
class StateMachine extends Client
{
    public function __construct()
    {
        parent::__construct();

        /* ... */

        $this
            ->addState($stateA)
            /* ... */
            ->createTransition('fourth', 'stateA', 'stateB')
            /* ... */
```

or

```
$stateMachine = new Client;
$stateMachine
    ->addState($stateA)
    ->addState($stateB)
    ->createTransition('fourth', 'stateA', 'stateB')
;
```

- Use Memento if you want do store current State and parameters and restore them later:

```
$memento = $stateMachine->createMemento();
```

and restore:

```
$stateMachine = new StateMachine();
$stateMachine->applyMemento($memento);
```

- That's all =) Your simple State machine is now configured and ready for use!

Example code:
-------------

[](#example-code)

```
