PHP code example of christianblos / symfony-di-annotation

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

    

christianblos / symfony-di-annotation example snippets



use Symfony\Component\DependencyInjection\Annotation\Compiler\AnnotationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerBuilder = new ContainerBuilder();

$srcDirs = ['path/to/classes']; // the path(s) to your classes which contain annotations

$containerBuilder->addCompilerPass(AnnotationPass::createDefault($srcDirs));


use Symfony\Component\DependencyInjection\Annotation\Service;

/**
 * @Service
 */
class SomeRepository
{

}


use Symfony\Component\DependencyInjection\Annotation\Service;

/**
 * @Service(public=true)
 */
class SomeService
{
    public function __construct(SomeRepository $repo)
    {
        // $repo will be injected automatically
    }
}

$someService = $container->get(SomeService::class);


use Symfony\Component\DependencyInjection\Annotation\Service;

/**
 * @Service(
 *     inject={
 *         "someParam"="%foo%"
 *     }
 * )
 */
class SomeService
{
    public function __construct($someParam)
    {
        // - "someParam" ist the name of the variable
        // - "%foo%" means you want to inject the "foo" parameter from the container
    }
}