项目作者: jascha030

项目描述 :
Simple Php dependency resolver
高级语言: PHP
项目地址: git://github.com/jascha030/DI-Container.git
创建时间: 2019-12-23T10:39:35Z
项目社区:https://github.com/jascha030/DI-Container

开源协议:

下载


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

  1. $ composer require jascha030/dic

Usage

Short example:

Class UserService with dependency User.

  1. class UserService
  2. {
  3. public $user;
  4. public function __construct(User $user)
  5. {
  6. $this->user = $user;
  7. }
  8. public function printUserName()
  9. {
  10. echo "My name is " . $this->user->name;
  11. }
  12. }
  1. $container = new PsrServiceContainer(); // Instantiate container
  2. $userService = $container->get(UserService::class); // Get service or class instance.
  3. $userService->printUserName(); // Outputs: My name is Jeff

Full example in src/example.php