PHPackages                             aaronjunker/sssm - 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. aaronjunker/sssm

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

aaronjunker/sssm
================

Super simple state machine

2.0.2(3y ago)213MITPHPPHP &gt;=8.0.0

Since Aug 18Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Aaron-Junker/sssm)[ Packagist](https://packagist.org/packages/aaronjunker/sssm)[ Docs](https://github.com/Aaron-Junker/sssm)[ Fund](https://github.com/sponsors/Aaron-Junker/)[ RSS](/packages/aaronjunker-sssm/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (2)Dependencies (1)Versions (6)Used By (0)

SSSM - Super simple state machine for PHP
=========================================

[](#sssm---super-simple-state-machine-for-php)

Report issues
-------------

[](#report-issues)

Please open a new issue to report a bug or request a new feature.

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

[](#installation)

```
composer require aaronjunker/sssm
```

View package on [packagist](https://packagist.org/packages/aaronjunker/sssm).

Usage
-----

[](#usage)

### 1. Include SSSM

[](#1-include-sssm)

```
include_once "vendor/autoload.php";

use sssm\State;
use sssm\StateMachine;
```

### 2. Create the states

[](#2-create-the-states)

Syntax:

```
$stateName = new state($stateName, $canLoop);
```

Both arguments are optional. But a name is highly recommended.

Example:

```
// State 1 named "State 1" (can loop(default))
$state1 = new state("State 1");
// State 2 named "State 2" (can loop)
$state2 = new state("State 2");
// State 3 named "State 3" (loop not allowed)
$state3 = new state("State 3", false);

// Allowed transitions from state 1 to state 2
$state1->addAllowedStateTransition($state2);
// Allowed transitions from state 2 to state 3
$state2->addAllowedStateTransition($state3);
// State 3 is not allowed to transition to any other state
```

### 3. Create the state machine

[](#3-create-the-state-machine)

Syntax:

```
$stateMachine = new stateMachine($initialState, $otherState, ...);
```

Example:

```
$stateMachine = new stateMachine($state1, $state2, $state3);
```

### 4. Add state events (optional)

[](#4-add-state-events-optional)

Syntax:

```
$stateName->onStateEnter[] = function(State $passedState) {
    // Things that get executed when entering the state
};

$stateName->onLoop[] = function(State $passedState){
    // Things that get executed when the state gets looped
};

$stateName->onStateLeave[] = function(State $passedState){
    // Things that get executed when leaving the state
};
```

Example:

```
$state1->onStateEnter[] = $state1->onStateEnter[] = function($state){
    echo $state->getStateName()." entered\n";
};

$state1->onLoop[] = function(state $state){
    echo $state->getStateName()." looped\n";
};

$state1->onStateLeave[] = function(state $state){
    echo $state->getStateName()." leaved\n";
};
```

### 5. Loop state

[](#5-loop-state)

Syntax:

```
$stateMachine->loop();
```

### 6. Switch state

[](#6-switch-state)

Syntax:

```
$stateMachine->switchState($newState);
```

Example:

```
$stateMachine->switchState($state2);
$stateMachine->switchState($state3);
```

### 7. Get current state

[](#7-get-current-state)

Syntax:

```
// Returns the current state class
$currentState = $stateMachine->getCurrentState();
```

Example:

```
echo $stateMachine->getCurrentState() === $state3?"State 3 is current state":"State 3 is not current state";
```

Full example
------------

[](#full-example)

```
