PHP Classes

Simple PHP Dependency Injection Container Class: Container to register and call service providers

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 86 All time: 10,018 This week: 94Up
Version License PHP version Categories
simple-ph-dic 1.0.2GNU General Publi...7.1Language, Design Patterns, PHP 7, PSR
Description 

Author

This package is a container to register and call service providers.

It can register a service provider taking as parameter the name of the service and a class that implements the service that is being provided.

The container that also return the object of a class that was registered as service provider, so applications can call its functions that implement the services that the class provides.

Picture of Pierre-Henry Soria
  Performance   Level  
Name: Pierre-Henry Soria <contact>
Classes: 49 packages by
Country: United Kingdom
Innovation award
Innovation award
Nominee: 18x

Winner: 3x

Documentation

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 ?

PHP Dependency Injection Container

Requirements ?

Installation ?

composer require ph-7/dependency-injection-container

Usage ?

Register your new DIC as below (FYI, for this example I use the Symfony's HttpFoundation Request).

For the first example, let's create your provider class with an anonymous class that implements the \PierreHenry\Container\Providable interface.

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... ?

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 I built for another side-project.

...Who Am I...? ?

Pierre-Henry Soria")

Hi there! ? I'm Pierre-Henry Soria, "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, 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 I started 8 years ago ??

License ?

Under GNU GPL v3 or later.


  Files folder image Files (11)  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file dependency-injection.svg Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  src  
File Role Description
Files folder imageException (3 files)
  Plain text file Container.php Class Class source
  Plain text file Providable.php Class Class source

  Files folder image Files (11)  /  src  /  Exception  
File Role Description
  Plain text file Container.php Class Class source
  Plain text file ContainerNotFound.php Class Class source
  Plain text file Provider.php Class Class source

  Files folder image Files (11)  /  tests  
File Role Description
  Plain text file ContainerTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:86
This week:0
All time:10,018
This week:94Up