3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Test; class SegmentContainer { private $fooSegmentArray = []; private $barSegmentArray = []; public function __construct(array $fooSegmentArray, array $barSegmentArray) { $this->fooSegmentArray = $fooSegmentArray; $this->barSegmentArray = $barSegmentArray; } public function popFooSegment() { return array_pop($this->fooSegmentArray); } public function shiftBarSegment() { return array_shift($this->barSegmentArray); } } /** * I provide a unified interface to reading the segments, one at a time */ interface SegmentReaderInterface { function readSegment(); } /** * I encapsulate a SegmentContainer, my derivatives determine how to * read from it. */ abstract class SegmentReaderBase implements SegmentReaderInterface { private $segmentContainer; public function __construct(SegmentContainer $segmentContainer) { $this->segmentContainer = $segmentContainer; } public function getSegmentContainer() { return $this->segmentContainer; } } class FooSegmentReader extends SegmentReaderBase { public function readSegment() { return $this->getSegmentContainer()->popFooSegment(); } } class BarSegmentReader extends SegmentReaderBase { public function readSegment() { return $this->getSegmentContainer()->shiftBarSegment(); } } /** * I don't care whether I end up reading from the $fooSegmentArray or * the $barSegmentArray, nor the direction from which I'm reading it. * I just want to be able to read them, one at a time. */ function acceptSegmentReader(SegmentReaderInterface $segmentReader) { var_dump($segmentReader->readSegment()); var_dump($segmentReader->readSegment()); } $segmentContainer = new SegmentContainer(['a', 'b', 'c'], ['1', '2', '3']); acceptSegmentReader(new FooSegmentReader($segmentContainer)); acceptSegmentReader(new BarSegmentReader($segmentContainer)); var_dump($segmentContainer);
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
string(1) "c" string(1) "b" string(1) "1" string(1) "2" object(Test\SegmentContainer)#1 (2) { ["fooSegmentArray":"Test\SegmentContainer":private]=> array(1) { [0]=> string(1) "a" } ["barSegmentArray":"Test\SegmentContainer":private]=> array(1) { [0]=> string(1) "3" } }
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/43gn2 on line 8
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/43gn2 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/43gn2 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/43gn2 on line 3
Process exited with code 255.

preferences:
49.31 ms | 411 KiB | 5 Q