Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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*
```php

anotherDependency = $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;
```