mkgor / simple-di
Simple implementation of dependency injection container on PHP
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
This package is auto-updated.
Last update: 2025-04-04 19:31:49 UTC
README
SimpleDI is a lightweight and easy to use dependency injection container for PHP.
Installation
You can install SimpleDI via composer
composer require mkgor/simple-di
That's it! You already can use it, because it need a little configuration to provide base functionality of DIC
How to use it?
Just call $container->get()
method and it'll resolve all dependencies of specified class and returns it to you!
SomeDependency.php
<?php class SomeDependency { public $anotherDependency; public function __construct(AnotherDependency $b) { $this->anotherDependency = $b; } }
AnotherDependency.php
<?php class AnotherDependency { public function sayHello() { echo "Hello from AnotherDependency"; } }
test.php
<?php require "vendor/autoload.php"; $container = new \SimpleDI\Container(__DIR__ . '/config.php'); //Output: Hello from AnotherDependency $container->get(SomeDependency::class)->anotherDependency->sayHello();
config.php
<?php return [ 'singleton' => [], 'definition' => [], ];
Getting class by alias
You can specify alias for some class and call it by that alias using SimpleDI
<?php return [ 'singleton' => [], 'definition' => [ 'aliasName' => [ 'classname' => SomeClass::class, 'arguments' => [] ] ], ];
<?php require "vendor/autoload.php"; $container = new \SimpleDI\Container(__DIR__ . '/config.php'); /** @var SomeClass $someClass */ $someClass = $container->get('aliasName');
Declaring singletons
If you have some class which will be created one time during request lifetime, you can declare it like singleton in configuration. SimpleDI will create its instance one time and save it in singletons container
<?php return [ 'singleton' => [ 'aliasForSingleton' => [ 'classname' => SomeSingleton::class, 'arguments' => [] ] ], 'definition' => [] ];
<?php require "vendor/autoload.php"; $container = new \SimpleDI\Container(__DIR__ . '/config.php'); /** @var SomeSingleton $someClass */ $someClass = $container->get('aliasForSingleton'); $someClass->a = 4; // Output: 4 echo $someClass->a; $someClass2 = $container->get('aliasForSingleton'); // Output: 4 echo $someClass2->a;