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;
use Yolo\Di\PropertyAttributeInterface;

#[Attribute]
class Cache implements 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),
        };
    }
}

DI::addCustomPropertyAttribute(Cache::class);

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

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

class TestRunner
{
    /**
     * @param LoggerInterface $logger It will be replaced by ConsoleLogger
     */
    public function __construct(private LoggerInterface $logger)
    {
    }

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

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

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

DI::alias(TestRunner::class, 'runner');
$runner = DI::use('runner');
$runner->sayHello();



namespace DI\Tests;

use Yolo\Di\Annotations\Initializer;

class UserTest
{
    public function __construct(private AnimalTest $animal)
    {
        echo "you can see me twice." . PHP_EOL;
    }

    #[Initializer]
    public function init()
    {
        echo "User.init" . PHP_EOL;
    }

    /**
     * Say hello
     * @return void
     */
    public function sayHello()
    {
        echo "Hello, I am a user." . PHP_EOL;
        $this->animal->sayHello();
    }
}



namespace DI\Tests;

use Yolo\Di\Annotations\Singleton;

#[Singleton]
class AnimalTest
{
    public function __construct()
    {
        echo 'you can only see me once' . PHP_EOL;
    }
    public function sayHello()
    {
        echo 'Hello, I am an animal.' . PHP_EOL;
    }
}



use Yolo\Di\DI;

'/LoggerInterface.php';
s TestRunner
{
    public function __construct(private LoggerInterface $logger)
    {
    }

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

$start = microtime(true);

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

// Use DI::alias to create an alias for a class
DI::alias(TestRunner::class, 'runner');
// And then use the alias to create an instance of TestRunner
$runner = DI::use('runner');
$runner->sayHello();

// Also, you can use the class name to create an instance of TestRunner
$runner = DI::use(TestRunner::class);
$runner->sayHello();

$end = microtime(true);

echo 'Spent ' . round(($end - $start) * 6, 3) . 'ms' . PHP_EOL;

echo 'Memory usage: ' . round(memory_get_usage() / 1024, 3) . 'kb' . PHP_EOL;