Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkgor/simple-di
SimpleDI is a lightweight and easy to use dependency injection container for PHP.
https://github.com/mkgor/simple-di
dependecy-injection di dic pattern php
Last synced: 7 days ago
JSON representation
SimpleDI is a lightweight and easy to use dependency injection container for PHP.
- Host: GitHub
- URL: https://github.com/mkgor/simple-di
- Owner: mkgor
- Created: 2020-04-30T18:51:19.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-04T09:14:43.000Z (over 4 years ago)
- Last Synced: 2024-04-17T17:20:37.668Z (7 months ago)
- Topics: dependecy-injection, di, dic, pattern, php
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SimpleDI
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*
```phpanotherDependency = $b;
}
}
```*AnotherDependency.php*
```php
get(SomeDependency::class)->anotherDependency->sayHello();
```*config.php*
```php
[],
'definition' => [],
];
```## Getting class by alias
You can specify alias for some class and call it by that alias using SimpleDI```php
[],'definition' => [
'aliasName' => [
'classname' => SomeClass::class,
'arguments' => []
]
],
];
``````php
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
[
'aliasForSingleton' => [
'classname' => SomeSingleton::class,
'arguments' => []
]
],'definition' => []
];
``````php
get('aliasForSingleton');
$someClass->a = 4;// Output: 4
echo $someClass->a;$someClass2 = $container->get('aliasForSingleton');
// Output: 4
echo $someClass2->a;
```