corex/ioc

This package is abandoned and no longer maintained. The author suggests using the corex/container package instead.

CoRex IoC

1.0.2 2019-03-17 15:30 UTC

This package is auto-updated.

Last update: 2020-04-15 05:14:25 UTC


README

License Build Status codecov

This is a simple container helping managing class dependencies and performing dependency injection.

Methods.

  • getInstance() - Get instance of container.
  • clear() - Clear container for all bindings and instances.
  • getBindings() - Get bindings.
  • getBinding() - Get specific binding.
  • has() - Check if class or interface has been bound.
  • hasInstance() - Check if class or interface has been instantiated.
  • isShared() - Check if class or interface is shared/is a singleton.
  • isSingleton() - Check if class or interface is shared/is a singleton.
  • forget() - Forget specified binding and instance.
  • bind() - Bind class or instance.
  • singleton() - Bind class or instance as shared/singleton.
  • instance() - Set instance on existing binding.
  • make() - Make instance of class or interface.

Create a container.

// Create new container.
$container = new Container();

// Create/use existing container.
$container = Container::getInstance();

Make a class without binding.

$myClass = Container::getInstance()->make(MyClass::class);

Make a class with binding and parameters.

$container = Container::getInstance();

$container->bind(MyClassInterface::class, MyClass::class);

$myClass = $container->make(MyClass::class, [
    'test' => 'my.value'
]);
  • When making a class via bound interface, the instance class will be checked if it implements the bound interface.
  • When making a class via bound base-class, the instance class will be checked if it extends the bound base class.