https://github.com/ph-7/simple-ph-dic
🌈 PH's DIC is a Simple PHP 7.1+ Dependency Injection Container (DIC) Library
https://github.com/ph-7/simple-ph-dic
clean-architecture clean-code clean-php container dependecy-injection dependency-injection dependency-injection-container di dic php php-71 php71 programming
Last synced: about 1 month ago
JSON representation
🌈 PH's DIC is a Simple PHP 7.1+ Dependency Injection Container (DIC) Library
- Host: GitHub
- URL: https://github.com/ph-7/simple-ph-dic
- Owner: pH-7
- License: gpl-3.0
- Created: 2018-09-02T23:29:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-27T00:41:59.000Z (4 months ago)
- Last Synced: 2025-04-09T20:04:28.611Z (about 1 month ago)
- Topics: clean-architecture, clean-code, clean-php, container, dependecy-injection, dependency-injection, dependency-injection-container, di, dic, php, php-71, php71, programming
- Language: PHP
- Homepage: https://github.com/pH-7/simple-PH-DIC
- Size: 31.3 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# P.H.'s Dependency Injection Container Library

## Summary ⏳
The "P.H.'s DIC" is a simple and lightweight PHP 7.1+ **Dependency Injection Container**'s library which lets you manage your dependencies easily for your next great project 🎉## Requirements ⚙
* 🐘 [PHP 7.1](http://php.net/releases/7_1_0.php) or newer
* 🎻 [Composer](https://getcomposer.org)## Installation 📓
```bash
composer require ph-7/dependency-injection-container
```## Usage 🎮
Register your new DIC as below (FYI, for this example I use the [Symfony's HttpFoundation](https://packagist.org/packages/symfony/http-foundation) Request).
For the first example, let's create your provider class with an [anonymous class](http://php.net/manual/en/language.oop5.anonymous.php) that implements the `\PierreHenry\Container\Providable` interface.
```php
use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;
use Symfony\Component\HttpFoundation\Request;$container = new Container();
// Register your container
$container->register(
'example.symfony.httprequest',
new class implements Providable
{
public function getService(): Request
{
return Request::createFromGlobals();
}
}
);// Retrieve the container
$httpRequest = $container->get('example.symfony.httprequest');// Use it
$request = $httpRequest->request; // $_POST body params
if ($request->get('get_var')) {
echo '$_POST["get var"] exists';
} else {
echo '"get_var" has not been requested';
}
```### Another Example... 🎯
```php
use DateTime;
use DateTimeZone;
use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;$container = new Container();
$container->register(
'stubs.date.datetime',
new class implements Providable
{
public function getService(): DateTime
{
return new DateTime('now', new DateTimeZone('America/Chicago'));
}
}
);// Retrieve the container
$date = $container->get('stubs.date.datetime');// Use it
echo $date->format('m-d-Y H:i:s');
```## Inspired By... 🧠
This project is highly inspired by my [DIC](https://github.com/Lifyzer/Lifyzer-WebApp-CMS/tree/master/Server/Core/Container) I built for another [side-project](https://lifyzer.com).
## ...Who Am I...? 🤔
[](http://pierrehenry.be "My personal website :-)")
Hi there! 😉 I'm [Pierre-Henry Soria](http://ph7.me), "[PierreHenry.be](http://pierrehenry.be)™": a cool passionate Belgian software engineer :belgium: :smiley:
You can keep in touch with me at *hi {{AT}} ph7 [[D0T]] me*!
## Passion Drives Me! 🚀
Love programming! 😊 Check out other exciting projects I previously did such as **[Lifyzer Healthy Food](https://github.com/Lifyzer)**, which gives lots of details about what you eat and allow you do scan your food items 🌯
Another project I'm proud of is the **[Social Dating WebApp Builder Project](https://github.com/pH7Software/)** I started 8 years ago ☀️## License ⚖
Under [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) or later.