1. Go to this page and download the library: Download djsharman/state library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
djsharman / state example snippets
//###START_CUSTOMCODE2
//###END_CUSTOMCODE2
interface DoorState
{
public function open();
public function close();
public function lock();
public function unlock();
}
abstract class AbstractDoorState implements DoorState
{
public function open()
{
throw new IllegalStateTransitionException;
}
public function close()
{
throw new IllegalStateTransitionException;
}
public function lock()
{
throw new IllegalStateTransitionException;
}
public function unlock()
{
throw new IllegalStateTransitionException;
}
}
class OpenDoorState extends AbstractDoorState
{
public function close()
{
return new ClosedDoorState;
}
}
class Door
{
private $state;
public function __construct(DoorState $state)
{
$this->setState($state);
}
public function open()
{
$this->setState($this->state->open());
}
public function close()
{
$this->setState($this->state->close());
}
public function lock()
{
$this->setState($this->state->lock());
}
public function unlock()
{
$this->setState($this->state->unlock());
}
private function setState(DoorState $state)
{
$this->state = $state;
}
}
r = new Door(new OpenDoorState);
var_dump($door->isOpen());
$door->close();
var_dump($door->isClosed());
$door->lock();
var_dump($door->isLocked());
$door->lock();
xml
<configuration>
<targetdir name="statemachines"/>
<namespace name="djsharman\examples\statemachines"/>
</configuration>