根据您的状态设置方式,听起来您可以设置一个带有工厂的类系统来优雅地处理所有这些?
您还可以使用状态检查设置您的类,您可能会抛出异常并且基本上无法实例化该类(因此无法进入该状态)。
我在想这样的事情可能对你有用:
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{
...
}
显然,这其中缺少很多东西,需要做很多事情才能将其变成您可以实际使用的东西,但是如果您像这样拆分它们,它就不会那么混乱,您将能够知道每个正在检查状态以及正在检查的内容。