Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

state-machine

> 编程语言
Open source

A very lightweight yet powerful PHP state machine ================================================= Define your states, define your transit…

524 stars0 likes0 views
WebsiteGitHub

About

A very lightweight yet powerful PHP state machine ================================================= Define your states, define your transit…

A very lightweight yet powerful PHP state machine

Define your states, define your transitions and your callbacks: we do the rest. The era of hard-coded states is over! Installation (via composer)

javascript
{
    "require": {
        "winzou/state-machine": "~0.1"
    }
}

Usage

Configure a state machine graph

In order to use the state machine, you first need to define a graph. A graph is a definition of states, transitions and optionnally callbacks ; all attached on an object from your domain. Multiple graphes can be attached to the same object.

Let's define a graph called myGraphA for our DomainObject object:

…

So, in the previous example, the graph has 6 possible states, and those can be achieved by applying some transitions to the object. For example, when creating a new DomainObject, you would apply the 'create' transition to the object, and after that the state of it would become pending.

Using the state machine

Definitions

The state machine is the object actually manipulating your object. By using the state machine you can test if a transition can be applied, actually apply a transition, retrieve the current state, etc. A state machine is specific to a couple object + graph. It means that if you want to manipulate another object, or the same object with another graph, you need another state machine.

The factory helps you to get the state machine for these couples object + graph. You give an object and a graph name to it, and it will return you the state machine for this couple. If you want to have this factory as a service in your Symfony2 application, please see the corresponding StateMachineBundle.

Usage

Please refer to the several examples in the examples folder.

Callbacks

Callbacks are used to guard transitions or execute some code before or after applying transitions.

Guarding callbacks must return a bool. If a guard returns false, a transition cannot be performed.

Credits

This library has been highly inspired by https://github.com/yohang/Finite, but has taken another direction.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言