PHP code example of kilahm / ioc-factory-container

1. Go to this page and download the library: Download kilahm/ioc-factory-container 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/ */

    

kilahm / ioc-factory-container example snippets


<?hh // strict

final class A
{
  <<provides('myA')>>
  public static function factory(FactoryContainer $c) : this
  {
    return new static();
  }
}

...
  <<__Memoize>>
  public function getMyA() : /Foo
  {
    return $this->newMyA();
  }
  
  public function newMyA() : /Foo
  {
    return $this->runner->make(class_meth('/A', 'factory'));
  }
...

<?hh // strict

namespace Foo\Baz;

use Foo\Bar\IFoo;

final class Foo implements IFoo
{
  <<provides('realFoo')>>
  public static function fooFactory(FactoryContainer $c) : this
  {
    return new static();
  }
  
  ...
  
}

<?hh // strict

namespace Bar;

use Foo\Bar\IFoo;

final class FooBar implements IFoo
{
  <<provides('fooBar')>>
  public static function barFactory(FactoryContainer $c) : this
  {
    return new static();
  }
  
  ...
  
}

...
  <<__Memoize>>
  public function getRealFoo() : \Foo\Baz\Foo
  {
    return $this->newRealFoo();
  }
  
  public function newRealFoo() : \Foo\Baz\Foo
  {
    return $this->runner->make(class_meth('\Foo\Baz\Foo', 'fooFactory'));
  }
  
  <<__Memoize>>
  public function getFooBar() : \Bar\FooBar
  {
    return $this->newFooBar();
  }
  
  public function newFooBar() : \Bar\FooBar
  {
    return $this->runner->make(class_meth('\Bar\FooBar', 'barFactory'));
  }
...

<?hh // strict

namespace CoolStuff;

use A;
use Foo\Bar\IFoo;

<<__ConsistentConstruct>>
class Awesome
{
  <<provides('awesomeWithFoo')>>
  public static function makeWithRealFoo(FactoryContainer $c) : this
  {
    return new static($c->getRealFoo(), $c->getA());
  }
  
  <<provides('awesomeWithBar')>>
  public static function makeWithFooBar(FactoryContainer $c) : this
  {
    return new static($c->getFooBar(), $c->getA());
  }
  
  public function __construct(private IFoo $foo, private A $a)
  {
  }