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 git.master, git.master_jit, rfc.property-hooks
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" } }

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:
25.24 ms | 406 KiB | 5 Q