I have a class with global $connection
but when I try to access it I am getting NULL. As you can see if i try to access within the constructor I do not get NULL. But from getConnection()
I am getting NULL.
class DatabaseManipulation
{
private $connection;
function __construct()
{
global $connection;
$connection = new mysqli("localhost", "root", "", "db");
$result = $connection->query("select * from user");
print_r($result); //I get an array
}
function getConnection(){
global $connection;
var_dump($connection); // I get -> object(DatabaseManipulation)#1 (1) { ["connection":"DatabaseManipulation":private]=> NULL } NULL
}
}
Same thing happens when I instantiate an object $connection = new DatabaseManipulation();
. Am I doing something wrong? I want this to be done in OO way. Can anyone help me ?