Dependency injection - is there an alternative to this very large constructor?

Soon, I decided to go for Dependency Injection technology for the new PHP application that I am going to write.

However, I'm not quite sure that I understand this enough. My problem is that baseClass is dependent on different elements, while many different classes will have to extend it. For instance:


namespace system;

class baseClass
{
 protected $logger;
 protected $filter;
 protected $database;
 protected $session;


 public function __construct(LoggerInterface $logger, filterInterface $filter, databaseInterface $database, sessionInterface $session)
 {
  $this->logger = $logger;
  $this->filter = $filter;
  $this->database = $database;
  $this->session = $session;
 }
}

Most classes will require a log, filter, etc. to complete its tasks: the filter class, for example, handles the screening / verification that I need throughout the project.

, , () baseClass, ( , , , , ) , .

, , "", :


$car = new Car($color,$amountOfWheels);

:


$car = new Car($logger, $filter, $database, $session, $color, $amountOfWheels);

, , , DI. , - factory , factory .?

+3
1

DbContext:

$dbContext = new DbContext($logger, $filter, $database, $session);

"":

$car = new Car($dbContext, $color, $amountOfWheels);
+4
source

Source: https://habr.com/ru/post/1772030/


All Articles