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 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
document published !
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 document published !
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'Document' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/RVVfZ on line 13
Process exited with code 255.

preferences:
162.33 ms | 402 KiB | 181 Q