PHP code example of genai / di

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

    

genai / di example snippets


$scanner = new GenAI\Attribute\Scanner($loader);
$scanner->scan(['App', 'GenAI\\Di\\Processor']);
$scanner->compile(new GenAI\Attribute\Context($configDir, $outputDir, $parameters));

#[Service]
class UserController {
    public function __construct(UserRepository $repo) {}   // autowired by type
}

#[Repository]
class UserRepository {}

#[Configuration]
class AppConfig {
    #[Bean(Mailer::class)]
    public function mailer(Clock $clock) {                 // params autowired too
        return new Mailer($clock);
    }
}

#[Value('${app.name}')]   // a config value — looked up in Context::$parameters and BAKED at build
#[Value('%db.dsn%')]      // a runtime container parameter -> $c->get('db.dsn')
#[Value('sendmail')]      // a literal

#[Bean(Renderer::class)]      // the attribute line is a comment on 5.3 — Type::class there is fine
public function renderer() {  // no ': Renderer'
    return new Renderer(__DIR__ . '/../templates');
}