Code Monkey home page Code Monkey logo

simple-ph-dic's Introduction

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 ๐Ÿ‡ง๐Ÿ‡ช ๐Ÿ˜ƒ

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.

simple-ph-dic's People

Contributors

ph-7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.