So, I'm old to PHP, but most of the time I've used procedural style. I've always tried to follow a MVC pattern, even though I was doing stuff procedurally. Also, I'm used to some frameworks like CakePHP and CodeIgniter. However, I've not yet done anything from scratch using OOP. Today I was doing so for a college work, and I got to a few questions.
Usually, in frameworks, we just do $this->database->query()
, on any controller class, and it works. On procedural, the $link
to the database goes global scope, so you don't have to worry. I was looking for a way to do that with every class on my code, but to no success. The best I could get was using a Singleton class to instantiate the database, however, I needed to retrieve the database link in every single class. So for instance, in class Users I'd have to get it in the constructor, and at the Main class, and in every other one.
My questions is fairly simple. How do I use the database connection link through multiple classes, without having to instantiate the db in all of them. And, if doing so is the best simpler way, is there any other way to do so than the Singleton method?
I'm using Mysqli
lib, by the way.
Here's the class I've been using.
class DB extends Mysqli {
protected static $instance;
private function __construct() {
global $config;
parent::__construct($config['db']['host'], $config['db']['user'], $config['db']['password'], $config['db']['database']);
if($this->connect_error)
die('Connect Error ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
public static function getInstance() {
if( !self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
Here is a class usage example.
class Main {
protected $db;
public function __construct() {
$this->db = DB::getInstance(); // <-- having to do that in every class is the problem
}
protected function start() {
$this->db->query(something...); // works
}
}