I have a question about using PDO that I haven't been able to find an answer to. This probably works the same for mysql/mysqli connections as well.
In the main include of my project, I create a PDO object using
$pdo = new PDO('connection here');
Well, I have a class that needs access to the database. So, instead of using "global $pdo;" inside of every function, I did the following.
class MyClass(){
private $db = null;
__construct(){
global $pdo;
$this->db = $pdo;
}
function example(){
$sql = 'A Query';
$this->db->prepare($sql);
}
}
Anyway, my question is, does doing this create 2 connections to the database since I'm effectively duplicating $pdo by setting the class' $db var equal to it? The main reason I ask is because I see this happening a lot in our system and am concerned with creating too many connections to MySQL and freaking the system out due to unnecessary connections.
As a part two, does the following cause duplication, and can I pass by ref? I'm a bit afraid to try it and cause something to break.
Change
function MyFunction($member_id, $pdo){
//do something.
}
To
function MyFunction($member_id, &$pdo){
//do something
}
Thanks!