PHP code example of liftkit / dependency-injection

1. Go to this page and download the library: Download liftkit/dependency-injection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liftkit / dependency-injection example snippets


use LiftKit\DependencyInjection\Container\Container;

$container = new Container;

$container->setRule(
  'SomeRule',
  function ()
  {
    return new SomeClass();
  }
);

$someObject = $container->getObject('SomeRule');

// $someObject will be an instance of SomeClass

$container->setSingletonRule(
  'SomeSingletonRule',
  function ()
  {
    return new SomeClass();
  }
);

$object1 = $container->getObject('SomeSingletonRule');
$object2 = $container->getObject('SomeSingletonRule');

// $object1 and $object2 are the same object

$container->setRule(
  'SomeRuleWithParameters',
  function (Container $container, $arg1, $arg2)
  {
    return new SomeClass($arg1, $arg2);
  }
);

$someObject = $container->getObject('SomeRuleWithParameters', ['arg1', 'arg2']);

// SomeClass will be contructed with 'arg1' and 'arg2' as the parameters to is constructor.

$container->setRule(
  'Rule1',
  function ()
  {
    return new SomeClass;
  }
);

$container->setRule(
  'Rule2',
  function (Container $container)
  {
    return new OtherClass($container->getObject('Rule1'));
  }
);

$someObject = $container->getObject('Rule2');

// $someObject will be an instance of OtherClass with a new instance of SomeClass injected as its
// first contructor argument.

$conatiner->setRule(
  'SomeRule',
  function ()
  {
    return new SomeClass;
  }
);

$container->setRule(
  'SomeRule',
  function ()
  {
    return new OtherClass;
  }
);

$someObject = $container->getObject('SomeRule');

// $someobject will be an instance of OtherClass

$someObject = new SomeObject;

$container->storeObject('SomeRule', $someObject);

$otherObject = $container->getObject('SomeRule');

// $someObject and $otherObject are the some object

class A
{
  private $b;
  
  public function __construct (B $b)
  {
    $this->b = $b;
  }
  
  public function getB ()
  {
    return $this->b;
  }
}

class B
{
  // placeholder class
}

$container->bindRuleToClass(
  'GiveMeANewA',
  A::class
);

$a = $container->getObject('GiveMeANewA');
$b = $a->getB();

// $a is a new instance of A. $b is a new instance of b.

$container->setRule(
  'GiveMeANewB',
  function ()
  {
    $b = new B;
    
    $b->createdByRule = true;
    
    return $b;
  }
);

$container->bindClassToRule(
  B::class,
  'GiveMeANewB'
);

$a = $container->getObject('GiveMeANewA');
$b = $a->getB();

// $b->createdByRule is true

class C
{
  private $b;
  private $param1;
  private $param2;
  
  public function __construct (B $b, $param1, $param2)
  {
    $this->b      = $b;
    $this->param1 = $param1;
    $this->param2 = $param2;
  }
  
  public function getB ()
  {
    return $this->b;
  }
  
  public function getParam1 ()
  {
    return $this->param1;
  }
  
  public function getParam2 ()
  {
    return $this->param2;
  }
}

$container->bindRuleToClass(
  'GiveMeANewC',
  C::class
);

$param1 = 1;
$param2 = 2;

$c = $container->getObject(
  'GiveMeANewC',
  [
    $param1,
    $param2,
  ]
);

$b = $c->getB();
$cParam1 = $c->getParam1();
$cParam2 = $c->getParam2();

// $b is an instance of B
// $cParam1 is 1
// $cParam2 is 2

class D extends B
{
  // placeholder class
}

$container->bindClassToAlias(
  B::class,
  D::class
);

$a = $container->getObject('GiveMeANewA');
$d = $a->getB();

// $d is an instance of D