public static function resolve($name) { $class = new ReflectionClass($name); $constructor = $class->getConstructor(); $resolved = []; if ($constructor !== null) { foreach ($constructor->getParameters() as $index => $param) { $paramType = $param->getClass()->name; if (isset(self::$list[$paramType]) === false) { if (class_exists($paramType) === false) { throw new Exception("DI couldn't resolve the dependency: " . $paramType); } array_push($resolved, DI::resolve($paramType)); } else { if (is_string(self::$list[$paramType])) { array_push($resolved, DI::resolve(self::$list[$paramType])); } else { $callable = self::$list[$paramType]; array_push($resolved, $callable()); } } } } return $class->newInstanceArgs($resolved); }
public function test_bind_function() { DI::bind('OtherModel', function () { return new OtherModel("my_string"); }); $instance = DI::resolve('MyFunction'); $this->assertInstanceOf('MyFunction', $instance); }