Purpose: to get something like this (pseudocode):

class Validation { private $db; public function __construct(DB $db) { $this->db = $db; } public function checkUnique() { $this->db->countUsers(); } } 

Conditional implementation:

 class DB { // переменные для подключения к БД: // protected: host, login, db, password public function __construct() { // Возвращение объекта подключения к БД DBConnect } public function checkDouble($table, $column, $email) { // тут мы сходили в базу return $table.'-'.$column.'-'.$email; } } class Validation { public function checkUnique($table, $column, $email) { $count = (new DB())->checkDouble($table, $column, $email); } } $obj = new DB(); $val = new Validation(); $check = $val->checkUnique('users', 'mail', 'email@domain.com'); 

I do not really understand what to pass to the Validation constructor in my case? There is no $ db variable here in Validation, but simply a class cannot be passed, for example, public function __construct(DB) writes there must be a variable. How then is the right thing to do?

  • $obj = new DB(); $val = new Validation($obj); ........ Can you go read the book better to start? at least ozon.ru/context/detail/id/139127353 - 500 pages ......... you can still ozon.ru/context/detail/id/137538198 and ozon.ru/context/detail/id/33506422 ...... and then books on patterns. And then you don’t have to ask such questions - Alexey Shimansky
  • @ Alexey Shimansky I have 2 of the 3 books listed. Well, it seems like it’s not forbidden to leave questions here ... - fosh4455
  • Not prohibited. But after reading these books from cover to cover and carefully studying them, such questions simply do not arise, because everything is described and demonstrated there. Especially the most banal fact - the transfer of arguments to the designer and indeed to any method - Alexey Shimansky

1 answer 1

di is a tool for introducing dependencies; there are already quite a few of them with their own features; there is a bit of information in psr https://github.com/php-fig/container

Your answer is written in pseudo code.

 class Validation { private $db; public function __construct(DB $db) { $this->db = $db; } public function checkUnique() { $this->db->countUsers(); } } $validation = new Validation(new DB()); $validation->checkUnique(); // но обычно при использование DI есть объект реализующий ContainerInterface (пусть будет $container) и пишется примерно так $validation = $container->get(Validator::class); $validation->checkUnique(); // Это был пример service locator. 

By the way, I have my own small project for the implementation of the di tool https://github.com/smpl/mydi

There is also such a thing as DIP (Dependecy Injection Principle) well, and this is also all based on aggregation and composition https://ru.wikipedia.org/wiki/%D0%90%D0B3% D1% 80% D0% B5% D0% B3% D0% B8% D1% 80% D0% BE% D0% B2% D0% B0% D0% BD% D0% B8% D0% B5 _ (% D0% BF% D1% 80% D0% BE% D0 % B3% D1% 80% D0% B0% D0% BC% D0% BC% D0% B8% D1% 80% D0% BE% D0% B2% D0% B0% D0% BD% D0% B8% D0% B5 )