PHP Classes


Recommend this page to a friend!
  Classes of AlexanderC  >  Pyha PHP Dependency Injection Container Using Traits  >  >  Download  
Role: Documentation
Content type: text/plain
Description: Documentation
Class: Pyha PHP Dependency Injection Container Using Traits
Create objects injecting dependencies using traits
Author: By
Last change: Update of
Date: 10 months ago
Size: 3,012 bytes


Class file image Download
What's dependency injection(DI)?

Just look at the article and watch that awesome video:

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
        new App(new AnyClassHere(), 'string here')
    // or
    // or (if app does not use injectable trait or do not get named param via getInjectables() method)
                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,
                             	 $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
For more information send a message to info at phpclasses dot org.