Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

jascha030/DI-Container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Php DI Container

Latest Stable Version Total Downloads Latest Unstable Version License composer.lock

About

Simple PSR-11 compliant dependency injection container.


Around the the web there are many examples to be found for simple PHP DI Containers and Resolvers. From all the inspiration I have written a version to my own personal liking. Feel free to use it in your project or take it as inspiration for your own version.

For now only the object definition has been implemented but more will follow.

Getting started

    $ composer require jascha030/dic

Usage

Short example:

Class UserService with dependency User.

class UserService
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function printUserName()
    {
        echo "My name is " . $this->user->name;
    }
}
$container = new PsrServiceContainer(); // Instantiate container

$userService = $container->get(UserService::class); // Get service or class instance.

$userService->printUserName(); // Outputs: My name is Jeff

Full example in src/example.php