Code Monkey home page Code Monkey logo

container's Introduction

๐Ÿ“ฆ DI Container

Yet another implementation of dependency injection container with PSR-11 compliant for PHP >8.0.

Installation

composer require please/container

Basic usage

This example always returns a new instance of Mailer.

use Please\Container\Container;
use Please\Container\Support\Getter;

$container = new Container;

$container->bind(Mailer::class, function (Getter $get) {
    return new Mailer::($get('user'), $get('password', 'qwerty')); // qwerty - default value
});

/** @var Mailer */
$mailer = $container->get(Mailer::class, [
  'user' => 'admin',
  // and password `qwerty` (default value)
]);

This example always return a same time.

use Please\Container\Container;

$container = new Container;

$container->singleton('random', fn () => rand());

echo $container->get('random'); // 1234567890
echo $container->get('random'); // 1234567890

Examples

You can find usage examples here.

Documentation

You can bind any abstract aliases.

use Please\Container\Container;

$container = new Container;

$container->bind(Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // error

// if you pass a class string
// it is also automatically bind as `Foo::class`
$container->bind('foo', Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok

// array of aliases
$container->bind([Foo::class, 'foo', 'another-foo-alias'], Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok
$container->get('another-foo-alias'); // ok

You can bind any primitive data types.

$container->bind('foo', 'bar');
$container->bind('foo', 1337);
$container->bind('foo', 13.37);
$container->bind('foo', true || false);
$container->bind('foo', ['bar', 'baz']);
$container->bind('foo', new stdClass);
$container->bind('foo', fopen('php://stdout', 'r'));

$container->bind('baz', null);
$container->get('baz') // returns `baz`

Generates a singleton instance of a class.

Singleton method always returns same value or instance like typical singleton pattern.

$container->singleton('timestamp', fn () => time());

// or use bind class and `shared` parameter
$container->bind('timestamp', fn () => time(), shared: true);

Check if the container has a binding or instance for the given abstract.

$container->bind('foo', 'bar');
$container->has('foo'); // true
$container->has('baz'); // false

Singleton Pattern

If you really need to, you can use the container as a singleton.

use Please\Container\Container as BaseContainer;
use Please\Container\Support\Traits\Singleton;

class Container extends BaseContainer
{
    use Singleton;
}

...

$container = Container::getInstance();

...

You can also check singleton example here.

License

Open-sourced software licensed under the MIT license.

container's People

Contributors

chipslays avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.