How do I work with the class database in any file?

There is a small class (file: db.php):
spoiler
class Db {

 protected $db;

 public function __construct() {
 $config = require 'config.php';
 $this->db = new PDO('mysql:host='.$config['host'].';charset=utf8;dbname='.$config['name'].", $config['user'], $config['password']);
}

 public function query($sql, $params = []) {
 echo '<pre>' . print_r($sql,true) . '</pre>';
exit;
 $stmt = $this->db->prepare($sql);
 if (!empty($params)) {
 foreach ($params as $key => $val) {
 if (is_int($val)) {
 $type = PDO::PARAM_INT;
 } else {
 $type = PDO::PARAM_STR;
}
 $stmt->bindValue(':'.$key, $val, $type);
}
}
$stmt->execute();
 return $stmt;
}

 public function row($sql, $params = []) {
 $result = $this->query($sql, $params);
 return $result->fetchAll(PDO::FETCH_ASSOC);
}

 public function column($sql, $params = []) {
 $result = $this->query($sql, $params);
 return $result->fetchColumn();
}

 public function lastInsertId() {
 return $this->db->lastInsertId();
}

}

I try to connect in your file (not working):
spoiler
include 'Db.php';

$DB = new Db;

addUser(123, 111111111);

function addUser($user_id, $date) {
 $params = [
 'user_id' => $user_id,
 'date' => $date,
];
 $DB->query('INSERT INTO users (user_id, date) VALUES (:name, :date)', $params);
 return $DB->lastInsertId();
}


Error: Fatal error: Uncaught Error: Call to a member function query() on null
April 19th 20 at 12:36
2 answers
April 19th 20 at 12:38
Solution
The idea is good, but the implementation is not.

To begin with, it is a very inconsistent class.
As they generally use, if the row() function returns more than one row, and all?
In the class a bunch of extra code, but 90% of the functionality of the PDO in it just not available

Class it is necessary to rewrite at least

class Db {
 public $db;

 public function __construct() {
 $config = require __DIR__.'/config.php'; 
 $options = [
 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
 PDO::ATTR_EMULATE_PREPARES => false,
];
 $dsn = 'mysql:host='.$config['host'].';charset=utf8;dbname='.$config['name'];
 $this->db = new PDO($dsn, $config['user'], $config['password'], $options);
}
 public function query($sql, $params = []) {
 $stmt = $this->db->prepare($sql);
$stmt->execute($params);
 return $stmt;
}
 public function cell($sql, $params = []) {
 return $this->query($sql, $params)->fetchColumn();
}
 public function row($sql, $params = [], $mode=PDO::FETCH_ASSOC) {
 return $this->query($sql, $params)->fetch($mode);
}
 public function all($sql, $params = [], $mode=PDO::FETCH_ASSOC) {
 return $this->query($sql, $params)->fetchAll($mode);
}
 public function column($sql, $params = []) {
 return $this->query($sql, $params)->fetchAll(PDO::FETCH_COLUMN);
}
 public function lastInsertId() {
 return $this->db->lastInsertId();
}
}

after ALL the functionality of PDO is added, it will be possible to make db back private. And to those x then, call it using the missing function body.

And connect the object of this class is a piece of cake: just to bring to the function

include 'Db.php';
$db = new Db;

addUser($db, 123, 111111111);

function addUser($db, $user_id, $date) {
 $params = [
 'user_id' => $user_id,
 'date' => $date,
];
 $db->query('INSERT INTO users (user_id, date) VALUES (:name, :date)', $params);
 return $db->lastInsertId();
}
Very cool, when you point out your mistakes, even if you didn't ask about it.
And the answer to the question, not just worked, and how to do it right.
For that special thank You! - Albertha90 commented on April 19th 20 at 12:41
April 19th 20 at 12:40
do require 'Db.php'
and do new Db()

Find more questions by tags PHP