I'm looking to implement user state / workflow handling in a PHP application.
Currently have:
Want to:
My research:
I checked both SO and other places for a PHP implementation of workflow and state machines and promising candidates seem to be
I'd be grateful for any comments regarding any experience with working with either of the libraries above and / or opinion regarding suitability for what I need or for hints to other places where to look to.
Depending on how your states are setup, sounds like you could just setup a class system with a factory to handle all of this elegantly?
You could also setup your classes with state checking, which you could throw exceptions and basically make it impossible to instantiate the class (and therefore impossible to enter that state).
I'm thinking something like this might work for you:
class StateFactory {
$currentState;
function __construct(){
if(!isset($_SESSION['currentState'])){
$this->currentState = 'StateOne';
}
else{
$this->currentState = $_SESSION['currentState'];
}
$this->currentState = new {$this->currentState}->processState(); // I think something like this will work
}
function __deconstruct(){
$_SESSION['currentState'] = $this->currentState;
}
}
abstract class State{
abstract function processState();
}
class StateOne extends State{
function processState(){
if(<check what is needed for this state>){
<do what you need to do for this state>
return 'StateTwo';
}
else
{
return 'StateWhatever';
}
}
}
class StateTwo extends State{
function processState(){
if(<check what is needed for this state>){
<do what you need to do for this state>
return 'StateThree';
}
else
{
return 'StateWhatever';
}
}
}
class StateThree extends State{
...
}
Obviously a lot is missing from this and a lot needs to be done to make this into something you can actually work with, but if you split things up like this, it will not be as messy and you'll be able to know where each state is being checked and what is being checked for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With