Answer the question
In order to leave comments, you need to log in
Am I using PHP-DI correctly?
DI itself - php-di.org
It seems that I understood the essence of the pattern, but did not understand how to apply it ...
For example, here is the code (Simplified and put together for an example):
<?php
require_once '../vendor/autoload.php';
use DI\ContainerBuilder;
$di = ContainerBuilder::buildDevContainer();
$di->set('db', new class {
public function getRow ()
{
return [
'name' => 'MyName',
'family' => 'MyFamily',
'age' => '17'
];
}
});
abstract class BaseController
{
protected $di;
public function __construct ($di)
{
$this->di = $di;
}
abstract function indexAction ();
}
class Controller extends BaseController
{
public function indexAction ()
{
$row = $this->di->get('db')->getRow();
// Пример всё таки, не прикручивать же для него шаблонизатор
echo 'Name: ',$row['name'],'<br> Family: ',$row['family'],'<br> Age: ',$row['age'];
}
}
$controller = new Controller($di);
$controller->indexAction();
$row = $this->di->get('db')->getRow();
$row = $this->db->getRow();
Answer the question
In order to leave comments, you need to log in
no, you misunderstood the idea. Passing the entire container as a dependency is a violation of this very pattern and the principle of dependency inversion. This can only be done in the case of cyclic dependencies, or simply as an alternative to lazy initialization of services (although the alternative is so-so, and cyclic dependencies should be avoided).
1) we read about the principle of dependency inversion
2) we read about dependency injection
3) if you already use nameless classes, then they must implement some interface or extend from some class.
Instead of what you want it should be:
class Foo {
private $db;
public function __construct(Connection $connection) {
$this->db = $connection;
}
public function makeFoo() {
return $this->db->getRows(); // как вы и хотели
}
}
// а это уже дергаем в конструкторе
$foo = $di->get(Foo::class);
$foo->makeFoo();
Not right.
Problem in line
$this->di = $di;
How to do it with PHP-DI? And is it even possible?Can. Get the object in the constructor.
Not right. Inject dependencies, not a container. You are breaking the Law of Demeter . There will be the same problems with the classic approach to unit testing, as if you did not inject anything at all and instantiated all dependencies right in the class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question