Skip to content

nishphp/simple-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-container

Build Status Coverage Status Code Climate

Latest Stable Version License

Simple container of auto wiring with constructor.

<?php
use Nish\Container\Container;

require_once 'vendor/autoload.php';

$c = Container::getInstance();
$obj = $c->get(stdClass::class);

$c->call(Foo::class, 'staticMethod');

$foo = new Foo();
$c->call($foo, 'method');

$c->setFactory('MyClass', function($c){
    return new MyClass('custom param');
});
$c->setFactory(MyClass::class, function($c){
    $obj = new MyClass('custom param');
    $c->set(MyClass::class, $obj); // singleton
    return $obj;
});

// set arguments
namespace MyProject;
class Db {
    private $dsn;
    public function __construct(string $dsn){
        $this->dsn = $dsn;
    }
    // ...
}
$c->set('MyProject\\Db#__construct.dsn', 'mysql://dbname...');
$db = $c->get(MyProject\\Db::class);

@see https://github.com/nishphp/simple-container/blob/master/test/ContainerTest.php

About

Simple Auto Wiring Container

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages