Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
PHP PDO Singleton usage in other classes
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 373013" data-attributes="member: 1872"><p>Right, basically I've got an MySQLi singleton wrapper which I want to use in other classes.</p><p></p><p>It is as follows:</p><p></p><p>[PHP]<?php</p><p>class Database extends PDO {</p><p> public static $instance;</p><p> private $connection;</p><p> </p><p> public function __construct() {</p><p> $this->connection = parent::__construct('mysql:host=localhost;dbname=lol', 'lol', 'lol');</p><p> }</p><p> </p><p> public static function getInstance() {</p><p> if (!isset(self::$instance)) {</p><p> self::$instance = new self();</p><p> }</p><p> </p><p> return self::$instance;</p><p> }</p><p>}[/PHP]</p><p></p><p>So, rather than doing</p><p>[php]$query = $db->prepare("SELECT......");[/php]</p><p>...and opening a new connection to the database each time, I do</p><p>[php]$query = $db::getInstance()->prepare("SELECT.......");[/php]</p><p>...which will check if a connection has already been established and just return it. Easier on the database</p><p></p><p>Now, I want to use this in other classes, for example:</p><p></p><p>[PHP]<?php</p><p>class Core {</p><p> private $_db;</p><p> </p><p> public function __construct(Database $db) {</p><p> if (!$db || $db->connect_errno > 0) {</p><p> die ('MySQLi connection error. Error sent from class.core.php');</p><p> } else {</p><p> $this->_db = $db;</p><p> }</p><p> }</p><p> </p><p> </p><p> public function AddLog($User, $Event) {</p><p> $log = $this->_db->prepare("INSERT INTO `Logs` (`UserID`, `Event`, `TimeStamp`, `IPAddress`) VALUES (?, ?, ?, ?);");</p><p> if ($log->execute([$User, $Event, time(), $_SERVER['REMOTE_ADDR']])) {</p><p> return true;</p><p> }</p><p> </p><p> return false;</p><p> }</p><p> </p><p>}[/PHP]</p><p></p><p>...but this does not allow me to to use my getInstance() method, how would I go about being able to do this?</p><p></p><p>Thanks,</p><p>Mark</p></blockquote><p></p>
[QUOTE="Markshall, post: 373013, member: 1872"] Right, basically I've got an MySQLi singleton wrapper which I want to use in other classes. It is as follows: [PHP]<?php class Database extends PDO { public static $instance; private $connection; public function __construct() { $this->connection = parent::__construct('mysql:host=localhost;dbname=lol', 'lol', 'lol'); } public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; } }[/PHP] So, rather than doing [php]$query = $db->prepare("SELECT......");[/php] ...and opening a new connection to the database each time, I do [php]$query = $db::getInstance()->prepare("SELECT.......");[/php] ...which will check if a connection has already been established and just return it. Easier on the database Now, I want to use this in other classes, for example: [PHP]<?php class Core { private $_db; public function __construct(Database $db) { if (!$db || $db->connect_errno > 0) { die ('MySQLi connection error. Error sent from class.core.php'); } else { $this->_db = $db; } } public function AddLog($User, $Event) { $log = $this->_db->prepare("INSERT INTO `Logs` (`UserID`, `Event`, `TimeStamp`, `IPAddress`) VALUES (?, ?, ?, ?);"); if ($log->execute([$User, $Event, time(), $_SERVER['REMOTE_ADDR']])) { return true; } return false; } }[/PHP] ...but this does not allow me to to use my getInstance() method, how would I go about being able to do this? Thanks, Mark [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP PDO Singleton usage in other classes
Top