class IoC {
protected static $registry = [];
public static function bind($name, Callable $resolver)
{
static::$registry[$name] = $resolver;
}
public static function make($name)
{
if (isset(static::$registry[$name]))
{
$resolver = static::$registry[$name];
return $resolver();
}
throw new Exception('Alias does not exist in the IoC registry.');
}
}
class Foo {}
class IoCTest extends PHPUnit_Framework_TestCase {
public function test_can_resolve_out_of_the_ioc_container()
{
IoC::bind('foo', function()
{
return new Foo;
});
$this->assertInstanceOf('Foo', IoC::make('foo'));
}
}
More than 5 years have passed since last update.
Inversion of Control (IoC) 仕組み分かりやすいPHPサンプル
Posted at
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme