PHP Classes

Pyha PHP Dependency Injection Container Using Traits: Create objects injecting dependencies using traits

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (2 months ago) RSS 2.0 feedNot enough user ratingsTotal: 459 All time: 6,082 This week: 314Up
Version License PHP version Categories
dependency-injection 1.0.1The PHP License5.4PHP 5, Language, Design Patterns
Description 

Author

This package can be used to create objects injecting dependencies using traits.

It can create a new object injecting dependencies using traits that define the injectable values or pass the values explicitly to the dependency injection container.

The new objects may be created as individual instances or retrieved as singleton objects.

Innovation Award
PHP Programming Innovation award nominee
February 2013
Number 2


Prize: One year subscription to the PDF edition of the JSMag magazine
Dependency injection is a design pattern that is used to create objects using external definitions that specify how the objects should be constructed and configured.

This package can inject dependencies by using traits to define the dependencies and injectable values during the object creating.

Manuel Lemos
Picture of AlexanderC
  Performance   Level  
Name: AlexanderC is available for providing paid consulting. Contact AlexanderC .
Classes: 13 packages by
Country: Moldova Moldova
Age: 34
All time rank: 7301 in Moldova Moldova
Week rank: 312 Up1 in Moldova Moldova Equal
Innovation award
Innovation award
Nominee: 3x

Details

What's dependency injection(DI)? ================================ Just look at the article and watch that awesome video: http://blog.ircmaxell.com/2013/01/dependency-injection-programming-with.html How do i register items(services)? ================================== If the class does not have any deps- just skip this step!!! First of all you have to register all dependencies, but keep in mind that: class App { // you can use this trait and implement getInjectables() // that returns associative array with named params that // could not be skipped or added with defined default values use Pyha\DI\Injectable; public function getInjectables() { return [ 'stringHere' => "hello world!" ]; } /** * @param string $stringHere */ public function __construct(AnyClassHere $thing, $stringHere) { } } class Foo { public function __construct(App $app) { } } class Bar extends Foo { } // i would like to get it via DI Factory class WeWannaGet { public function __construct(Bar $bar) { } } // in order to get this working we need... // // 1. register only top dependency Pyha\DI\Factory::getInstance()->register( new App(new AnyClassHere(), 'string here') ); // or Pyha\DI\Factory::getInstance()->register( Pyha\DI\Factory::getInstance()->get('App') ); // or (if app does not use injectable trait or do not get named param via getInjectables() method) Pyha\DI\Factory::getInstance()->register( Pyha\DI\Factory::getInstance()->get('App', ['stringHere' => "hello world!"]) ); // that u can use Pyha\DI\Factory::getInstance()->get('WeWannaGet'); // to get instance only once, just what an singleton does Pyha\DI\Factory::getInstance()->create('WeWannaGet'); // to create new instance each time To register aliases use addAlias() Factory method. NOTE: if as a service parameter to the register method is provided an object by default are registered several service aliases. This are: - All Interfaces names implemented - All parent classes from extension tree - All Traits used If you do not wannt to register this aliases, take a look at the method prototype: public function register($name, $service, $registerSelfAsAlias = true, $registerInterfacesAsAliases = true, $registerParentsAsAliases = true); Services ca be objects, closures, string(classes names) Loading Library =============== If you are not using Composer you may add to autoloader the lib path or even include the files manualy Installing via Composer ======================= To install it via composer just use following command: $ composer.phar install

  Files folder image Files  
File Role Description
Files folder imagelib (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  lib  
File Role Description
Files folder imagePyha (2 directories)

  Files folder image Files  /  lib  /  Pyha  
File Role Description
Files folder imageDI (4 files)
Files folder imageHelpers (1 directory)

  Files folder image Files  /  lib  /  Pyha  /  DI  
File Role Description
  Plain text file Container.php Class Class source
  Plain text file Factory.php Class Class source
  Plain text file Injectable.php Class Class source
  Plain text file Service.php Class Class source

  Files folder image Files  /  lib  /  Pyha  /  Helpers  
File Role Description
Files folder imagetraits (1 file)

  Files folder image Files  /  lib  /  Pyha  /  Helpers  /  traits  
File Role Description
  Plain text file Singleton.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:459
This week:0
All time:6,082
This week:314Up