PHP code example of yolo-fx / di

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

    

yolo-fx / di example snippets


use Yolo\Di\DI;

DI::bind(LoggerInterface::class, ConsoleLogger::class);

$logger = DI::use(LoggerInterface::class);
$logger->log("Hello, World!");

DI::alias(TestRunner::class, 'runner');

$runner = DI::use('runner');
$runner->sayHello();

#[Attribute]
class Cache implements \Yolo\Di\PropertyAttributeInterface
{
    public function __construct(private string $driver = 'default') {}

    public function inject(): mixed
    {
        return match ($this->driver) {
            'file' => DI::use(FileLogger::class),
            default => DI::use(ConsoleLogger::class),
        };
    }
}

class TestRunner
{
    public function __construct(
        #[Cache('file')]
        private LoggerInterface $logger
    ) {}

    public function sayHello(): void
    {
        $this->logger->log("Hello, World!");
    }
}