0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Inversion of Control (IoC) 仕組み分かりやすいPHPサンプル

Posted at

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'));
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?