3v4l.org

run code in 300+ PHP versions simultaneously
<?php # basic container class Container { private $raw; // callbacks private $values; // objects private $locked; public function __construct(array $config = []) { foreach($config as $id => $callback){ $this->set($id, $callback); } } public function set(string $id, callable $callback): void { if(isset($this->locked[$id])){ throw new LockedIdException(); } $this->raw[$id] = $callback; } public function get(string $id) { if(!isset($this->raw[$id])){ throw new UnknownIdException(); } if(isset($this->values[$id])){ return $this->values[$id]; } $cb = $this->raw[$id]; $val = $this->values[$id] = $cb($this); $this->locked[$id] = true; return $val; } public function has(string $id): bool { return isset($this->values[$id]); } } class Resolver { public function __construct(Container $container, WireRule ...$rules) { $this->c = $container; $this->rules = $rules; } public function get(string $class) { if(!$this->c->has($class)){ $this->c->set($class, $this->resolve($class)); } return $this->c->get($class); } public function resolve(string $class): callable { if(!class_exists($class)){ throw new UnknownIdException(); } if(!method_exists($class, '__construct')){ return function(Container $c) use ($class){ return new $class; }; } $constructor = new \ReflectionMethod($class, '__construct'); if(count($params = $constructor->getParameters()) === 0){ return function(Container $c) use ($class){ return new $class; }; } foreach($params as $param){ $args[] = $param->hasType() ? $this->getArgument($param->getType()->__toString()) : null; } return function(Container $c) use ($class, $args){ return new $class(...$args); }; } public function getArgument(string $type) { foreach($this->rules as $rule){ if($rule->match($type)){ $type = $rule->getWiredClass($type); } } return $type ? $this->get($type) : null; } } class Foo { public function __construct(Bar $bar){ $this->bar = $bar; }} class Bar { public function __construct(Baz $baz){ $this->baz = $baz; }} class Joe { public $bob = 'bob'; } class Baz {} $c = new Resolver(new Container); var_dump($c->get(Foo::class)); var_dump($c->get(Baz::class)); var_dump($c->get(Joe::class));
Output for git.master_jit, git.master, rfc.property-hooks
Deprecated: Creation of dynamic property Resolver::$c is deprecated in /in/M01FL on line 52 Deprecated: Creation of dynamic property Resolver::$rules is deprecated in /in/M01FL on line 53 Deprecated: Creation of dynamic property Bar::$baz is deprecated in /in/M01FL on line 102 Deprecated: Creation of dynamic property Foo::$bar is deprecated in /in/M01FL on line 101 object(Foo)#4 (1) { ["bar"]=> object(Bar)#6 (1) { ["baz"]=> object(Baz)#8 (0) { } } } object(Baz)#8 (0) { } object(Joe)#10 (1) { ["bob"]=> string(3) "bob" }

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