Skip to content

rleroi/SimpleDi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleDi

Simple dependency injection

Uses phpdoc's @property to automatically inject missing properties.

Usage:

/**
 * @property \App\World $world
 **/
class MyClass {
  use SimpleDi;

  function hello() {
    // 'world' property is automatically injected as a new \App\World()
    var_dump($this->world);
  }
}

Singleton

// create an instance that should be injected every time for \App\World
$world = new \App\World('param');
SimpleDiService::singleton(\App\World::class, $world);


/**
 * @property \App\World $world
 **/
class MyClass {
  use SimpleDi;

  function hello() {
    // 'world' property is automatically injected as the \App\World
    // instance defined previously in SimpleDiService::singleton();
    var_dump($this->world);
  }
}

Bind

// Create a callback that returns an instance that should be injected for \App\World
SimpleDiService::bind(\App\World::class, fn () => new \App\World('param'));

/**
 * @property \App\World $world
 **/
class MyClass {
  use SimpleDi;

  function hello() {
    // 'world' property is automatically injected as defined previously in SimpleDiService::bind();
    var_dump($this->world);
  }
}

About

Simple dependency injection PoC in PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages