There is a connection to the database:

class Db { public static function getConnection() { $paramsPath = ROOT . '/config/db_params.php'; $params = include($paramsPath); $dsn = "mysql:host={$params['host']};dbname={$params['dbname']}"; $db = new PDO($dsn, $params['user'], $params['password']); $db->exec("set names utf8"); return $db; } } 

And in every class, in every method I do this:

 public static function getCategoriesList() { $db = Db::getConnection(); $result = $db->query('SELECT id, name FROM category ' . 'ORDER BY sort_order ASC'); ......... } 

How to get rid of this govnokod? $db = Db::getConnection();

How can I call a connection in the index file and not connect every time in each method? As an option to create a connection class in other classes to knock on it through the constructor?

  • It will be correct here like this class DB extends \PDO {...} if you write / use your wrapper over PDO. - Visman

1 answer 1

Just save the connection in a static variable:

 private static $db = null; public static function getConnection() { if (!is_null(self::$db)) return self::$db; // ... self::$db = ...; return self::$db; } 

However, it is capable of delivering pain as soon as the need arises for a second connection.

In your case (static classes), about the same can be done in client classes:

 class Advt { private static $db; public static function setDB(DB $db){self::$db = $db;} public static function getAdvtList($page = 1) { //... self::$db->query(...); //... } } Advt::setDB(DB::getConnection()); 

Everything below is just my vision of how it should be.

It is better to store the connection in a DB instance, and by implementing a couple of methods for queries, work with it.
In classes whose existence without a connection to the base does not make sense, you can pass the base object to the constructor and save it in $this->db say.


 class Db { private $config; private $connection; public function __construct() { $paramsPath = ROOT . '/config/db_params.php'; $this->config = include($paramsPath); } public function connect() { $dsn = "mysql:host={$this->config['host']};dbname={$this->config['dbname']}"; $this->connection = new PDO($dsn, $this->config['user'], $this->config['password']); $this->connection->exec("set names utf8"); } public function query($sql, $params) { if (!$this->connection) $this->connect(); // prepare/execute } public function rows($sql, $params) { // query/fetch } // fetch rows public function row($sql, $params) {} // fetch one row public function col($sql, $params, $col = 0) {} // fetch column public function cell($sql, $params, $col = 0) {} // fetch scalar value } 

somecontroller.php

 class A { protected $db; public function __construct(DB $db) {$this->db = $db;} public function getUser($id) { return $this->db->row('SELECT * FROM Users WHERE id = ?', [$id]); } } 
  • Thanks for the answer. But if you do not implement methods for queries to the Db class, and do everything in the methods of the models. These methods will be static and $ this-> will not work there. Here is your example in class A to pass an object to the constructor and use it in static getUser ($ id). - Misha Rodnoy
  • And the connect method in the class bd is static too - Misha Rodnoy
  • The use of purely static classes is basically meaningless. - vp_arth
  • In the main part of my answer, there was no $this - vp_arth
  • one
    Thank you for your attention. I think to rewrite everything without static classes and methods. - Misha Rodnoy