PHP code example of ekaterinadavidovich / container

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

    

ekaterinadavidovich / container example snippets




class Сooker implements СookerInterface
{
    // some code
}

class Fridge implements FridgeInterface
{
    // some code
}

class Kitchen
{
    public function __construct(
        protected СookerInterface $cooker,
        protected FridgeInterface $fridge,
        protected int $area,
        )
    {      
    }

    public function getArea(): int
    {
        return $this->area;
    }
}



use Container\Container;

$container = new Container();

$container->add('kitchenArea', 666)->addTag('apartment');

$container->add(СookerInterface::class, 
        fn() => new Сooker()
    )->addTag('apartment');

$container->add(FridgeInterface::class, 
        fn() => new Fridge()
    )->addTag('apartment');

$container->add(Kitchen::class, 
        fn() => new Kitchen(
            $container->get(СookerInterface::class),
            $container->get(FridgeInterface::class),
            $container->get('kitchenArea'),
        )
    )->addTag('apartment');



if ($container->get(Kitchen::class)->getArea() === 666) {
    echo 'You have successfully injected the dependency!';
}