I have a database connection class, in which i am using a constructor for connection. I have another class(class Product). How do I get the $conn variable from the class database connection into the class products so that I can use the variable for executing my queries ? . Here in my code I want get the database connection variable from class DatabaseCon ($this->conn) into the class Products method getProducts() so that i can use it in mysqli_query(connection variable here,$sql). Currently I am inheriting the class DatabaseCon into class Products and it is working but i want to know if this is the correct way of doing it or not.
<?php
class DatabaseCon
{
function __construct()
{
**$this->conn** = mysqli_connect("localhost","root","qwerty","mss");
if($this->conn)
{
// echo "Connected";
}
else
{
echo "Error : Not connected";
}
}
}
?>
<?php
require 'db_config.php';
class Products extends DatabaseCon
{
function getProducts()
{
$sql= "SELECT * FROM products";
$row= mysqli_query(**$this->conn**,$sql);
$result= mysqli_fetch_all($row,MYSQLI_ASSOC);
return $result;
}
}
?>