3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Request { public function __construct() { echo 'Request created', PHP_EOL; } } class DI { protected $shared = []; protected $definitions = []; public function __construct() {} public function set($name, $definition, $shared = false) { if($shared) { if(isset($shared[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->shared[$name] = $definition; } else { if(isset($definitions[$name])) { throw new Exception("Definition '$name' is already set. Use DI::remove('$name') to remove it."); } $this->definitions[$name] = $definition; } } public function getRaw($name) { if(!isset($this->definitions[$name])) { throw new Exception("Definition '$name' is not set. Use DI::set('$name', <definition>, <shared>) to set it."); } return $this->definitions[$name]; } public function get($name) { $definition = $this->getRaw($name); echo is_callable($definition) ? 'Callable def' : 'Non-calleable def'; echo class_exists($definition) ? 'Class' : 'non-class'; die; return $definition(); } } $di = new DI(); //Using an anonymous function $di->set('request1', function(){ return new Request(); }); //Using an anonymous function $di->set('request2', 'Request'); var_dump($di->get('request1'));
based on nNLAd
Output for 5.4.0 - 5.6.28, 7.0.0 - 7.1.0
Callable def Warning: class_exists() expects parameter 1 to be string, object given in /in/g4SYi on line 40 non-class
Output for hhvm-3.12.0
Callable def Catchable fatal error: Object of class Closure$get;65557089$24a281d8f160e674d0dfbe06b92421a6$ could not be converted to string in /in/g4SYi on line 40
Process exited with code 255.
Output for hhvm-3.10.0
Callable def Catchable fatal error: Object of class Closure$get;65557089$9e0c28e78b50d65d69ed969541d81a7$ could not be converted to string in /in/g4SYi on line 40
Process exited with code 255.
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/g4SYi on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/g4SYi on line 10
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/g4SYi on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/g4SYi on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/g4SYi on line 4
Process exited with code 255.