3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StatePatternPHP; interface DocumentManagement { public function review(); public function approve(); public function reject(); public function publish(); } abstract class State implements DocumentManagement { protected Document $document; public function __construct(Document $document) { $this->document = $document; } public function review() { throw new Exception("Document cannot be reviewed in this current state (". get_class($this).")"); } public function approve() { throw new Exception("Document cannot be approved in this current state (". get_class($this).")"); } public function publish() { throw new Exception("Document cannot be published in this current state (". get_class($this).")"); } public function reject() { throw new Exception("Document cannot be rejected in this current state (". get_class($this).")"); } } class Document implements DocumentManagement { private string $content; private State $currentState; private int $approvals = 0; public function __construct(string $content) { $this->content = $content; $this->currentState = new Draft($this); } public function getContent(): string { return $this->content; } public function setContent(string $content){ $this->content = $content; $this->currentState = new Draft($this); $this->approvals = 0; } public function setState(State $state){ $this->currentState = $state; } public function addApproval(){ $this->approvals++; } public function disapprove(){ $this->approvals--; } public function getApprovals(){ return $this->approvals; } public function review() { $this->currentState->review(); } public function approve() { $this->currentState->approve(); } public function publish() { $this->currentState->publish(); } public function reject() { $this->currentState->reject(); } } class Draft extends State { public function review() { $this->document->setState(new InReview($this->document)); } } class InReview extends State { public function approve() { $this->document->addApproval(); } public function publish() { if($this->document->getApprovals() > 2){ //needs 3 votes at least $this->document->setState(new Published($this->document)); }else{ parent::publish(); } } public function reject() { $this->document->disapprove(); } } class Published extends State { public function __construct(Document $document) { parent::__construct($document); print('document published !'); } } $document = new Document("hello world !"); $document->review(); $document->approve(); $document->approve(); $document->approve(); $document->publish();
Output for git.master, git.master_jit, rfc.property-hooks
document published !

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
62.93 ms | 401 KiB | 8 Q