PHP code example of sportlog / di

1. Go to this page and download the library: Download sportlog/di 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/ */

    

sportlog / di example snippets

 php


log\DI\Container;

// Given this class and interface:
interface FooInterface
{
    public function getFoo(): string;
}

class Foo implements FooInterface
{
    public function __construct(private ?string $foo = 'foo')
    {
    }

    public function getFoo(): string
    {
        return $this->foo;
    }
}

// 1) You can simply get the instance via class id
$container = new Container();
$foo = $container->get(Foo::class);
echo $foo->getFoo();    // outputs: "foo"

// 2) When working with interfaces you must set
// the class id which shall be created
$container = new Container();
$container->set(FooInterface::class, Foo::class);
$foo = $container->get(FooInterface::class);
echo $foo->getFoo();    // outputs: "foo"

// 3) You can also use a factory. Parameters are
// getting injected
class Config
{
    public function getFooInit(): string
    {
        return 'init-foo';
    }
}

$container = new Container();
// Instance of Config will be injected to the factory
$container->set(
    FooInterface::class,
    fn (Config $config) => new Foo($config->getFooInit())
);
$foo = $container->get(FooInterface::class);
echo $foo->getFoo();    // outputs: "init-foo"