Reputation:
I am attempting to place a commonly used method (opendb) in the same class and file as my connect configuration file (connect.php) See fig A
class qcon{
public static $conn;
function dbcon()
{
if (empty($conn))
{
$host = 'x';
$username = 'x';
$password = 'x';
$dbname = 'x';
$conn = mysqli_connect($host , $username , $password ,$dbname) or die("Oops! Please check SQL connection settings");
}
return $conn;
}
function openDB($conn)
{
if (!$conn)
{
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
Now I want to be able to pass the connection output of fig A so I can properly use the methods in another class file. Call it class.php. Here's one example function on class.php for viewing records. See fig. B
require_once("assets/configs/connect.php");
class dbcats {
var $conn;
function getResult(){
$result = mysqli_query($this->conn , "SELECT * from felines" );
if ($result) {
return $result;
}
else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}
function closeDB() {
mysqli_close($this->conn);
}
Now, to get the call to work, Fig C below is where I'm at. I'm a tiny bit stuck.
$db1 = new qcon();
$helper = new dbcats();
$db1->openDB();
$helper = $db1;
$result = $helper->getResult();
So here we are. The logic is simple enough (I'll update the question if I'm not quite clear) So would someone advise on what amendments I need to get the call operational?
Upvotes: 2
Views: 94
Reputation: 2687
Orangepill's solution is fine and will more than likely get you going quickly. However, I would recommend making some minor alterations to your classes to allow you to more easily reuse their functionality across your programs.
Here, qcon
holds your database connection information and functionality. It has a getter method, getConn()
that allows you to get, and pass around that connection as you need.
class qcon
{
protected $conn;
public function __construct() { ... }
public function dbcon() { ... }
public function openDB() { ... }
public function closeDB() { ... }
public function getConn() { return $this->conn; }
}
Here's an example of an alternative dbcats
class. It takes in your qcon
class in part of its construction and holds it in as a protected member variable that it can use whenever it needs it. It also has getters and setters so that you can change or retrieve your database connection for this class at any time via getQConn()
and setQConn()
.
class dbcats
{
protected $qcon;
public function __construct(qcon $q) { $this->qcon = $q; }
public function getResult() { ... }
public function getQConn() { return $this->qcon; }
public function setQCon(qcon $q) { $this->qcon = $q; }
}
It may not be the quickest fix, but I believe practices such as this will serve you better in the long-run.
Upvotes: 2
Reputation: 24645
You need to inject an instance of the qcon class into the dbcats class.
require_once("assets/configs/connect.php");
class dbcats {
var $conn;
public function __construct(qcon $dbconn){
$this->conn = $dbconn;
}
function getResult(){
$result = mysqli_query($this->conn , "SELECT * from felines" );
if ($result) {
return $result;
}
else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}
function closeDB() {
mysqli_close($this->conn);
}
}
Then when you create an instance of dbcats pass in an instance of conn like ...
$db1 = new qcon();
$db1->openDB();
$helper = new dbcats($db1);
$result = $helper->getResult();
Upvotes: 0
Reputation: 3073
From what I can see you are missing the end curly bracket } on both your classes. Everything would be much easier to see if you make the indentation correctly. That is, each time you have a left curly bracket { the following lines will be indented with one tab. Like this:
class qcon {
public static $conn;
function dbcon()
{
if (empty($conn))
{
$host = 'x';
$username = 'x';
$password = 'x';
$dbname = 'x';
$conn = mysqli_connect($host , $username , $password ,$dbname) or die("Oops! Please check SQL connection settings");
}
return $conn;
}
function openDB($conn)
{
if (!$conn)
{
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
} <<< Missing this one
class dbcats {
var $conn;
function getResult(){
$result = mysqli_query($this->conn , "SELECT * from felines" );
if ($result) {
return $result;
}
else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}
function closeDB() {
mysqli_close($this->conn);
}
} <<< Missing this one
Upvotes: 0