I doubt that is there any state machine framework like https://github.com/pluginaweek/state_machine for PHP.
I've had to define many if-else logical clauses, and I would like something to help make it more fun by just defining:
- Condition required to transition
- State after transition
Then this can be reused to check if conditions match or not, for example
$customer->transition('platinum');
I expect this line of code to implicitly check if the customer can transition or not. Or explicitly check by:
$customer->canTransitTo('platinum');
Thanks in advance, noomz
For the record, I write (yet another) state machine
https://github.com/EFTEC/StateMachineOne
However, it's based in automation rather of event (however it is possible to use a field as event). The transition of state is triggered by values, manually or by code.
For example, the parking example:
where pedal, turnkey, gas, speed and brake are fields defined and controlled by code.
So, it is even possible to program advanced features such as what if the car runs out of gas or even modify values.
Features:
I also published an article about it
https://medium.com/cook-php/creating-a-state-machine-using-php-ddef9395430e
FSM Package and FSM Parser.
I've been working on a simple PHP finite state machine library, kind of similar to the rails state_machine. The code is here: https://github.com/chriswoodford/techne/tree/v0.1
A car example, similar to the selected answer (above) would look something like this:
Initialization
Usage
It lacks an explicitly defined interface that since it uses the __call() magic method to accept messages, but that could easily be resolved with an adapter.
I don't know a framework like this (which doesn't mean it does not exist). But while not as feature packed as the linked framework, the State pattern is rather simple to implement. Consider this naive implementation below:
After you defined the states, you just have to apply them to your main object:
And then you can do
For reducing overly large if/else statements, this should be sufficient. Note that returning a new state instance on each transition is somewhat inefficient. Like I said, it's a naive implementation to illustrate the point.
I've written a state machine for php. I'm sure you've figured out a solution long ago for this. But for people visiting this page, you are welcome to try out this state machine.
https://github.com/definitely246/state-machine
To use it, you define transition event handlers as classes. Then you need to define transitions. The finite state machine can be configured to do other stuff, but here are the basics.
You can then define transitions that change states when an event is triggered.
You can install with composer
I've created Smalldb framework to implement model layer of a web application using state machines. It is designed to work directly on SQL database, where each row of the SQL table represents an instance of a state machine (so the state machines are persistent).
It has role-based access control, so you can specify in state machine definition which transitions are allowed to which users (it also can deal with owners of the state machine instance or components of more complex entities).
To make development faster Smalldb can load state diagrams in GraphML created with yEd editor, so you can draw a state chart and then directly use it in your application as well as in your documentation. Also there is a native editor (jQuery widget + desktop wrapper) under development. For debugging purposes and online in-app documentation the Smalldb can generate state diagrams using Graphviz.