<?php
class Request {
public function __construct() {
echo 'Request created';
}
}
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);
return $definition();
}
}
$di = new DI();
//Using an anonymous function
$di->set('request', function(){
return new Request();
});
var_dump($di->get('request'));
Parse error: syntax error, unexpected '[' in /in/Fo6Gu on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/Fo6Gu 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/Fo6Gu on line 4
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Fo6Gu 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/Fo6Gu on line 4
Process exited with code 255.