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();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0120.00918.27
8.3.50.0140.00018.12
8.3.40.0080.00820.30
8.3.30.0150.00318.75
8.3.20.0080.00020.89
8.3.10.0050.00320.39
8.3.00.0000.00822.39
8.2.180.0070.00718.30
8.2.170.0070.00722.96
8.2.160.0000.01420.96
8.2.150.0000.00824.18
8.2.140.0000.00824.66
8.2.130.0060.00326.16
8.2.120.0040.00421.18
8.2.110.0080.00320.49
8.2.100.0090.00317.84
8.2.90.0050.00517.91
8.2.80.0030.00517.97
8.2.70.0090.00017.93
8.2.60.0060.00318.16
8.2.50.0000.00818.10
8.2.40.0040.00419.30
8.2.30.0070.00019.25
8.2.20.0030.00618.26
8.2.10.0000.00719.29
8.2.00.0030.00519.29
8.1.280.0090.00625.92
8.1.270.0100.00020.48
8.1.260.0060.00326.35
8.1.250.0000.00728.09
8.1.240.0040.00420.71
8.1.230.0110.00022.82
8.1.220.0040.00417.79
8.1.210.0000.00918.79
8.1.200.0040.00417.36
8.1.190.0000.00917.23
8.1.180.0090.00018.77
8.1.170.0060.00318.54
8.1.160.0000.00819.05
8.1.150.0020.00518.93
8.1.140.0040.00418.90
8.1.130.0000.00720.03
8.1.120.0000.00817.46
8.1.110.0040.00417.48
8.1.100.0040.00417.54
8.1.90.0040.00417.50
8.1.80.0040.00417.38
8.1.70.0040.00417.50
8.1.60.0000.00817.68
8.1.50.0080.00017.56
8.1.40.0000.00817.43
8.1.30.0000.00817.70
8.1.20.0080.00017.57
8.1.10.0040.00417.63
8.1.00.0030.00917.40
8.0.300.0030.00519.92
8.0.290.0060.00316.88
8.0.280.0040.00418.45
8.0.270.0000.00716.93
8.0.260.0030.00320.11
8.0.250.0000.00717.04
8.0.240.0000.00717.10
8.0.230.0030.00317.07
8.0.220.0050.00216.96
8.0.210.0000.00816.99
8.0.200.0030.00617.05
8.0.190.0020.00516.99
8.0.180.0040.00417.03
8.0.170.0030.00616.98
8.0.160.0040.00416.82
8.0.150.0000.00716.89
8.0.140.0000.00716.84
8.0.130.0030.00313.40
8.0.120.0000.00716.99
8.0.110.0060.00316.92
8.0.100.0070.00016.97
8.0.90.0090.00016.98
8.0.80.0120.00716.97
8.0.70.0040.00417.02
8.0.60.0070.00016.98
8.0.50.0040.00416.93
8.0.30.0090.01316.89
8.0.20.0070.01117.16
8.0.10.0040.00416.97
8.0.00.0140.00416.85
7.4.330.0040.00015.55
7.4.320.0060.00016.51
7.4.300.0000.00816.54
7.4.290.0000.00816.60
7.4.280.0050.00316.48
7.4.270.0080.00316.59
7.4.260.0060.00013.29
7.4.250.0000.00816.44
7.4.240.0020.00516.58
7.4.230.0000.00716.63
7.4.220.0030.00316.46
7.4.210.0050.01116.54
7.4.200.0000.00716.70
7.4.130.0140.00316.54
7.4.120.0100.00716.68
7.4.110.0170.00016.57
7.4.100.0030.01316.36
7.4.90.0170.00616.41
7.4.80.0130.00519.39
7.4.70.0100.00716.32
7.4.60.0120.00616.59
7.4.50.0070.01116.29
7.4.40.0140.00316.55
7.4.30.0070.01016.68
7.4.20.0070.01316.61
7.4.10.0080.00816.61
7.4.00.0130.00916.62
7.3.330.0030.00416.23
7.3.320.0000.00512.99
7.3.310.0030.00316.27
7.3.300.0060.00016.27
7.3.290.0070.00816.15
7.3.260.0100.00916.24
7.3.240.0080.00916.23
7.3.230.0110.00916.36
7.3.210.0120.00916.11
7.3.200.0050.01416.23
7.3.190.0110.00716.11
7.3.180.0080.00816.21
7.3.170.0090.01316.09
7.3.160.0060.00916.16
7.3.150.0090.00616.14
7.3.140.0160.00616.29
7.3.130.0030.01316.27
7.3.120.0070.01316.18
7.3.110.0090.00616.21
7.3.100.0080.00816.05
7.3.90.0130.00716.18
7.3.80.0150.00316.21
7.3.70.0090.01216.16
7.3.60.0100.01316.21
7.3.50.0100.01316.02
7.3.40.0160.00316.18
7.3.30.0150.00616.17
7.3.20.0070.00716.13
7.3.10.0090.00916.24
7.3.00.0180.00416.26
7.2.330.0040.01416.21
7.2.320.0090.00616.31
7.2.310.0120.00616.25
7.2.300.0140.00316.31
7.2.290.0150.00616.59
7.2.280.0030.01316.40
7.2.270.0130.00916.41
7.2.260.0090.01216.38
7.2.250.0110.00816.39
7.2.240.0130.00916.33
7.2.230.0070.01416.49
7.2.220.0120.00916.23
7.2.210.0130.01016.39
7.2.200.0120.01216.38
7.2.190.0090.01216.25
7.2.180.0140.00816.40
7.2.170.0110.01116.20
7.2.160.0090.00916.38
7.2.150.0130.00716.41
7.2.140.0080.00816.23
7.2.130.0070.01016.41
7.2.120.0200.00316.41
7.2.110.0090.01516.43
7.2.100.0070.01316.14
7.2.90.0070.01116.20
7.2.80.0080.01216.14
7.2.70.0080.01616.48
7.2.60.0090.01216.42
7.2.50.0090.01516.28
7.2.40.0210.00016.56
7.2.30.0120.01216.29
7.2.20.0060.01616.15
7.2.10.0130.01016.37
7.2.00.0050.01316.38

preferences:
64.15 ms | 400 KiB | 5 Q