2017 © Pedro Peláez
 

library container

A lightweight PHP dependency injection container.

image

flashytime/container

A lightweight PHP dependency injection container.

  • Wednesday, July 11, 2018
  • by flashytime
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Container

A lightweight PHP dependency injection container., (*1)

Installation

composer require flashytime/container

Usage

Create a container
use Flashytime\Container\Container;
$container = new Container();
Register a closure
$this->container->set('hello', function () {
    return 'Hello World!';
});
echo $this->container->get('hello'); //Hello World!
$this->container->set('name', function () {
    return 'Mocha';
});
$this->container->set('mocha', function ($container) {
    return sprintf('Hello %s!', $container->get('name'));
});
echo $this->container->get('mocha'); //Hello Mocha!



##### Register a instance or class ```php $this->container->set('foo', function () { return new Foo(); });
or
$this->container->set('foo', new Foo());
or
$this->container->set('foo', 'Flashytime\Container\Tests\Foo');
or
$this->container->set('foo', Flashytime\Container\Tests\Foo::class);
##### Get the entry
$this->container->get('foo');
##### Register a singleton
$this->container->setSingleton('foo', Flashytime\Container\Tests\Foo::class);
$first = $this->container->get('foo');
$second = $this->container->get('foo');
var_dump($first === $second); //true
##### Dependency Injection
interface FooInterface
{

}

class Foo implements FooInterface
{
    private $name;
    private $age;

    public function __construct($name = null, $age = 0)
    {
        $this->name = $name;
        $this->age = $age;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setAge($age)
    {
        $this->age = $age;
    }

    public function getAge()
    {
        return $this->age;
    }
}

class Bar
{
    public $foo;

    public function __construct(FooInterface $foo)
    {
        $this->foo = $foo;
    }

    public function getFoo()
    {
        return $this->foo;
    }
}
```php $this->container->set(FooInterface::class, Foo::class); $this->container->set('bar', Bar::class); $bar = $this->container->get('bar'); var_dump($bar instanceof Bar); //true var_dump($bar->getFoo() instanceof Foo); //true

see tests to get more usages., (*2)

License

MIT, (*3)

The Versions

11/07 2018

dev-master

9999999-dev

A lightweight PHP dependency injection container.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar flashytime

container dependency-injection di

11/07 2018

v1.0.0

1.0.0.0

A lightweight PHP dependency injection container.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar flashytime

container dependency-injection di