3v4l.org

run code in 300+ PHP versions simultaneously
<?php class locator { public $instances; public function setDef($name, $instance){ $this->instances[$name] = $instance; } public function get($name){ if(isset($this->instances[$name])){ $o = (new ReflectionClass($name))->getConstructor(); if($o === null || null === $o->getParameters()){ return new $this->instances[$name](); } else { $p = $o->getParameters(); $instancesArray = []; foreach($p as $i){ $instancesArray[] = $this->get($i->getClass()->name); } $inst = new ReflectionClass($this->instances[$name]); return $inst->newInstanceArgs($instancesArray); } } } } class z { public $z = "zzzzzz"; } class base { public $b = "base !!!"; } class second { public $z; public $s = "second !!!"; public function __construct(z $a){ $this->z = $a; } } class a { public $base; public $second; public function __construct(base $b, second $s){ $this->base = $b; $this->second = $s; } } $loc = new locator(); $loc->setDef('z', z::class); $loc->setDef('second', second::class); $loc->setDef('base', base::class); $loc->setDef('a', a::class); $o = $loc->get('a'); echo $o->base->b; echo "<br>"; echo $o->second->s; echo "<br>"; echo $o->second->z->z; ?>
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Method ReflectionParameter::getClass() is deprecated in /in/VgRC1 on line 16 Deprecated: Method ReflectionParameter::getClass() is deprecated in /in/VgRC1 on line 16 Deprecated: Method ReflectionParameter::getClass() is deprecated in /in/VgRC1 on line 16 base !!!<br>second !!!<br>zzzzzz

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:
19.57 ms | 402 KiB | 8 Q