2017 © Pedro Peláez
 

library dependency-injection

Simple Dependency Injection in php

image

sixx/dependency-injection

Simple Dependency Injection in php

  • Tuesday, October 4, 2016
  • by sapsan4eg
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Sixx DependencyInjection

Simple Dependency Injection for you php project, (*1)

INSTALLATION WITH COMPOSER

composer require sixx/dependency-injection, (*2)

EXAMPLE

Constructor Injection

<?php
    require_once __DIR__ . "/vendor/autoload.php";

class FirstClass
{
    protected $test;

    public function __construct(TestInterface $test)
    {
        $this->test = $test;
    }

    public function getTest()
    {
        return $this->test;
    }
}

interface TestInterface
{
}

class Test implements TestInterface
{
}

use \Sixx\DependencyInjection\Inject;
// For first you must bind your injection
Inject::bind("TestInterface", "Test");

$class = Inject::instantiation("FirstClass");
var_dump($class->getTest() instanceof Test); // must return true

Initializer methods

<?php
require_once __DIR__ . "/vendor/autoload.php";

class FirstClass
{
    protected $test;

    public function test(TestInterface $test)
    {
        $this->test = $test;
        return $this;
    }

    public function getTest()
    {
        return $this->test;
    }
}

interface TestInterface
{
}

class Test implements TestInterface
{
}

use \Sixx\DependencyInjection\Inject;
// For first you must bind your injection
Inject::bindByArray(["TestInterface" => "Test"]);

$class = Inject::method("FirstClass", "test");
var_dump($class->getTest() instanceof Test); // must return true

Initializer properties

Property must be public and must have attribute @var with needed Class or Interface name, (*3)

<?php
require_once __DIR__ . "/vendor/autoload.php";

class FirstClass
{
    /**
     * @var TestInterface
     */
    public $test;

    public function getTest()
    {
        return $this->test;
    }
}

interface TestInterface
{
}

class Test implements TestInterface
{
}

use \Sixx\DependencyInjection\Inject;
// For first you must bind your injection
Inject::bindByArray(["TestInterface" => "Test"]);

$class = Inject::instantiation("FirstClass");
var_dump($class->getTest() instanceof Test); // must return true

Service Locator

<?php
require_once __DIR__ . "/vendor/autoload.php";

class FirstClass implements FirstClassInterface
{
    /**
     * @var TestInterface
     */
    public $test;

    public function getTest()
    {
        return $this->test;
    }
}

interface FirstClassInterface
{

}

interface TestInterface
{
}

class Test implements TestInterface
{
}

use \Sixx\DependencyInjection\Inject;
// For first you must bind your injection
Inject::bindByArray(["TestInterface" => "Test", "FirstClassInterface" => "FirstClass"]);

$class = Inject::instantiation("FirstClassInterface");
var_dump($class instanceof FirstClass); // must return true

Different classes to one interface via DocComment

<?php
require_once __DIR__ . "/vendor/autoload.php";

interface SomeInterface
{

}

class First implements SomeInterface
{

}

class Second implements SomeInterface
{

}

class A
{
    /**
     * @thisAnnotationForSecondClass
     * @var SomeInterface
     */
    public $secondVariable;

    protected $firstVariable;

    /**
     * A constructor.
     * @thisAnnotatnionForFristClass
     * @param SomeInterface $my
     */
    public function __construct(SomeInterface $my)
    {
        $this->firstVariable = $my;
    }

    public function getFirst()
    {
        return $this->firstVariable;
    }
}

use \Sixx\DependencyInjection\Inject;
Inject::bindByArray(["SomeInterface" => ["thisAnnotatnionForFristClass" => "First", "thisAnnotationForSecondClass" => "Second"]]);

$class = Inject::instantiation("A");
var_dump($class->secondVariable instanceof Second); // must return true
var_dump($class->getFirst() instanceof First); // must return true

The Versions

04/10 2016

dev-master

9999999-dev https://github.com/sapsan4eg/DependencyInjection

Simple Dependency Injection in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Yuri Nasyrov

dependency injection php ioc

31/05 2016

0.0.3

0.0.3.0 https://github.com/sapsan4eg/DependencyInjection

Simple Dependency Injection in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Yuri Nasyrov

dependency injection php ioc

11/05 2016

0.0.2

0.0.2.0 https://github.com/sapsan4eg/DependencyInjection

Simple Dependency Injection in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Yuri Nasyrov

dependency injection php ioc

27/03 2016

0.0.1

0.0.1.0 https://github.com/sapsan4eg/DependencyInjection

Simple Dependency Injection in php

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Yuri Nasyrov

dependency injection php ioc