There is a FrontController

<?php class FrontController { // Свойства класса private static $_instance = null; private $_db; protected $_controller, $_action, $_params, $_body; // Приватный конструктор для соединения с базой данных private function __construct() { try { $this -> _db = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME , DBUSER, DBPASS); $this -> _db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo 'Коннект к базе'; } catch(PDOException $e) { die($e -> getMessage()); } } // Метод, запускающий приложение public function run() { $url = !empty($_GET['action']) ? explode('/', trim($_GET['action'], '/')) : null; $this -> _controller = !empty($url[0]) ? preg_replace('#[^az]#i', '', $url[0]) . 'Controller' : null; $this -> _action = !empty($url[1]) ?preg_replace('#[^az]#i', '', $url[1]) : null; $ctrl = 'IndexController'; $action = 'index'; if(class_exists($this -> _controller)) { $ctrl = $this -> _controller; if(method_exists($ctrl, $this -> _action)) $action = $this -> _action; } $controller = new $ctrl; $controller -> $action(); } // Статичный метод static public function getInstance() { if(self::$_instance == null) self::$_instance = new self(); return self::$_instance; } public function view($file) { include_once VIEWS . 'header.php'; include_once VIEWS . $file . '.php'; include_once VIEWS . 'footer.php'; } // Запрещаем метод "Клон" private function __clone(){} 

}

It is inherited by the other controllers. I create an IndexController, which is the default. Now I don’t understand one thing ... How can I get access to the $ _db property in other controllers in order to continue to make queries to the database? or am I writing something wrong? thank

  • And why do you need $ _db in the controller? O_o - dekameron
  • and how best to do? recently started learning OOP ... MVC ... - vinnie
  • 2
    Work with the database should be in the model, for that it is a model. And the controller talks to the model and should not have a clue about the existence of the database. - zhenyab pm
  • Got it !!!!!!!! - vinnie

0