Giter Club home page Giter Club logo

php-di's Introduction

php-module

Definitions

// create container
$container = new Container();

// bind and define with class name 
$container->set(Database::class);

// define with class name and bind with contract class
$container->set(CartContract::class, Cart::class);

// define with function and bind with contract class
$container->set(CommerceContract::class, function () {
    return new Commerce();
});

$db = $container->get(Database::class)

Values

$container->set('db.host', 'localhost');
$container->set('db.port', 5432);
$container->set('db.user', 'root');
$container->set('db.pass', 'pass');

$container->set(Database::class, function () {
    return new Database(
        $container->get('db.host'),
        $container->get('db.port'),
        $container->get('db.user'),
        $container->get('db.pass'),
    )
});

Singleton

// singaton definitions
$container->singleton(Logger::class);

$a = $container->get(Logger::class);
$b = $container->get(Logger::class);

// $a === $b

Factory

$container->set(Product::class);

$a = $container->get(Product::class);
$b = $container->get(Product::class);

// $a != $b

Auto Injection

class Database {
}

class ProductRepo {
    public function construct(private Database $db) {
    }
}

$container->set(Database::class);
$container->set(ProductRepo::class); // auto autowired

Autowired

$container->set(Logger::class);

// auto inject during define function
$container->set(Database::class, function (Logger $logger) {
    return new Database($logger);
});

Call

$container->set(Database::class);

$findUserById = function (Database $db, int $id): User {
    return new User($db->row('user', ['id' => $id]));
}

$user = $container->call($findUserById, ['id' => 3]);

Define

class Cache {
    public function construct(private Logger $logger, private string $path) {
    }
}
$container->set(Logger::class);
$container->set(Cache::class, new Define(Cache::class));
$cache = $container->get(Cache::class)->resolve(['path' => __DIR__]);
class User {
    public function construct(private $name = 'guest') {
    }
    public function getName(): string {
        return $this->name;
    }
}
$container->set(new Define(User::class));
$userA = $container->get(User::class)->resolve();
$userB = $container->get(User::class)->resolve(['name' => 'Mlax']);

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.