1. Go to this page and download the library: Download webdevcave/yadic 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/ */
webdevcave / yadic example snippets
ebdevcave\Yadic\Annotations\Provides;
use Webdevcave\Yadic\Annotations\Singleton;
use Webdevcave\Yadic\ServiceContainer;
interface StorageInterface
{
public function store(mixed $data): bool;
}
#[Provides('storage')]
#[Provides(StorageInterface::class)]
#[Singleton]
class Storage implements StorageInterface
{
public function store(mixed $data): bool
{
//store data...
return true;
}
}
class MyController
{
public function __construct(
private StorageInterface $storage
) {
}
public function save(): bool
{
return $this->storage->store('my data...');
}
}
$container = new ServiceContainer();
//No need to do this in a real world application:
$container->addAlias(StorageInterface::class, Storage::class);
//Use this instead:
//$container->loadDefinitionsFromDirectory($directory, $namespace); //Loads annotations from classes declared in a PSR4 directory
//var_dump($container->get('storage')->store($data));
var_dump($container->get(MyController::class)->save()); //bool(true)