Answer the question
In order to leave comments, you need to log in
How to use Di container?
Just yesterday I was introduced to the DI container and the documentation seems to be clear and there are a lot of examples, but it doesn’t reach me.
There is
class App {
private $companyService;
public function __construct() {
$container = new Container;
$container->add('CompanyCreateService', CompanyCreateService::class)
->addArgument(CompanySqlRepository::class);
$container->add(CompanySqlRepository::class)
->addArgument(DbConnection::class)
->addArgument(Hydrator::class);
$container->add(CompanySqlRepository::class);
$container->add(DbConnection::class);
$container->add(Hydrator::class);
$container->add(AdminController::class);
$container->add('AdminController', AdminController::class)->addArgument('CompanyCreateService');
$this->companyService = $container->get('AdminController');
$this->actionIndex();
}
public function actionIndex() {
$controller = new AdminController();
}
class AdminController {
public $companyService;
public function __construct(CompanyServiceInterface $companyService) {
$this->companyService = $companyService;
$this->actionCreate();
}
public function actionCreate() {
$dto = [...]
$this->companyService->createCompany($dto);
echo 'saved';
App
I run AdminController
without parameters. Fatal error: Uncaught ArgumentCountError: Too few arguments to function
, which is logical, since I do not pass any parameter to the constructor. But at the same time, the data is still saved to the database as it should. $container->get('что-нибудь');
Answer the question
In order to leave comments, you need to log in
I recommend reading a book about the pattern or looking at this site , in particular strategy patterns. Then I think everything will become much clearer, since now you even have the wrong questions, but in general it’s better not to get into such jungle without experience.
In short, then:
We have a container, in which we register classes (services) when the application starts. When registering, we associate an abstraction (interface) and an implementation (concrete class), and basically tell our application HOW to create a concrete object. Then we just do it and get the finished object
// Получаем объект интерфейса из любой точки приложения
$container->get(MyInterface::class);
$container->register(MyInterface::class, function () {
return new MyConcreteClass('какие то параметры');
});
interface CartRepository
{
public function add(CartItem $item);
public function getAll();
}
class SessionCartRepository implements CartRepository
{
...
}
class DbCartRepository implements CartRepository
{
...
}
class Cart
{
public function __construct(CartRepository $repository)
{
...
}
public function getTotal()
{
...
}
}
// Service provider
$container->register(CartRepository::class, function ($container) {
if ($user = $container->get('auth')->getUser()) {
$repository = new DbCartRepository($container->get('db'), $user->id)
} else {
$repository = new SessionCartRepository();
}
return $repository;
});
$container->register(Cart::class, function ($container) {
return new Cart($container->get(CartRepository::class));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question