3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface Creatable { } class FooCreatable implements Creatable { } class BarCreatable implements Creatable { } interface Creator { /** * * @param string $input * @param Creatable $creatable * @return bool */ function tryCreate($input, Creatable &$creatable = null); } class AggregateCreator implements Creator { /** * * @var Creator[] */ private $creators = []; /** * * @param Creator[] $creators */ public function __construct($creators) { $this->creators = $creators; } /** * * @param string $input * @param Creatable $creatable * @return bool */ public function tryCreate($input, Creatable &$creatable = null) { foreach ($this->creators as $creator) { if ($creator->tryCreate($input, $creatable)) { return true; } } return false; } } class FooCreator implements Creator { /** * * @param string $input * @param Creatable $creatable * @return bool */ public function tryCreate($input, Creatable &$creatable = null) { if ($input == 'foo') { $creatable = new FooCreatable; return true; } return false; } } class BarCreator implements Creator { /** * * @param string $input * @param Creatable $creatable * @return bool */ public function tryCreate($input, Creatable &$creatable = null) { if ($input == 'bar') { $creatable = new BarCreatable; return true; } return false; } } $creator = new AggregateCreator([ new FooCreator, new BarCreator, ]); $creatable = null; var_dump($creator->tryCreate('foo', $creatable), $creatable); $creatable = null; var_dump($creator->tryCreate('bar', $creatable), $creatable); $creatable = null; var_dump($creator->tryCreate('qux', $creatable), $creatable);
Output for git.master, git.master_jit, rfc.property-hooks
bool(true) object(FooCreatable)#4 (0) { } bool(true) object(BarCreatable)#4 (0) { } bool(false) NULL

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:
45.77 ms | 401 KiB | 8 Q