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