Dependency Injection with PHP-DI

How to use PHP-DI in SysVector.

SysVector uses the PHP-DI library to manage dependency injection. This allows you to write more modular and testable code.

Basic Configuration

To configure PHP-DI, you can create a di-config.php file with the definitions of your dependencies:


use DI\ContainerBuilder;

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        // Define your dependencies here
        ClientRepository::class => \DI\create(MySqlClientRepository::class),
    ]);
};
    

Usage

To use dependency injection in your classes, simply pass the dependencies through the constructor:


class ClientService {
    private $clientRepository;
    
    public function __construct(ClientRepository $clientRepository) {
        $this->clientRepository = $clientRepository;
    }
    
    public function getClients() {
        return $this->clientRepository->findAll();
    }
}
    

With dependency injection, you can easily swap implementations and keep your code decoupled and more testable.





Contact

If you're interested in SysVector, get in touch:

Donations via PAYPAL

If you wish to support the development of SysVector, consider making a donation via PAYPAL to Donate via PayPal