Skip to content

Library to introduce myself with the concept of Dependency Injection in php

Notifications You must be signed in to change notification settings

lilianQ-Q/DependencyInjectionManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

DIM stands for DependencyInjectionManager. It has been developped from the followed article https://dev.to/emrancu/let-s-create-php-dependency-injection-container-24lm.

I did this to train myself with the concept of dependency injection. It will now be used in my "Everate" Framework.

Usage

Retrieve a class

To create a class with the DI container you can process like this :

use DIM\DependencyInjectionContainer;

$container = DependencyInjectionContainer::instance();
$class = $container->make("class with namespace here");

$class->method();

Execute a method

Let's assume we have the following class

namespace DIM;

use Dim\Http\Request;
use Dim\Http\Response;

class UserController
{
    public function addToCart(Request $request, Response $response) : Response
    {
        if ($request->isGood())
            return ($response->success());
        return ($response->failed());
    }

    public function test(int $number) : void
    {
        echo "Trying to echo a number throught DI : $number";
    }
}

But you dont want to instantiate all manually. Then you can ask the container to automaticly call methods that don't need a values in parameters.

use DIM\DependencyInjectionContainer;

$container = DependencyInjectionContainer::instance();

//First way
$container->call("UserController@addToCart");

//Second way
$container->call(["UserController", "test"], ["number" => 4]);

About

Library to introduce myself with the concept of Dependency Injection in php

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published