I am new to OOP PHP, and I've been working with procedural API since I've started Web Development so I'm having a hard time migrating to OOP.
so let's say I have this four .php
file and structures below.
connection.db.php
<?php
define("DB_HOST", "127.0.0.1");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "sample_db");
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
echo (!$db->connect_error) ? NULL : die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
?>
sampleclass.class.php
<?php
public $db;
class MySQLqueries {
public function samplefunction($queryString) {
$sqlQry = mysqli->query($queryString);
return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
}
}
?>
includes.inc.php
<?php
error_reporting(0);
date_default_timezone_set("Asia/Manila");
require 'connection.db.php';
require 'sampleclass.class.php';
?>
index.php
<?php
require 'includes.inc.php';
$todo = new MySQLqueries;
echo $todo->samplefunction("SELECT `sample_column` FROM `sample_table` WHERE `sample_column` = 'sample_value';");
?>
As you may or may have not noticed, my problem is how to use the $db
from connection.db.php
in the samplefunction
of sampleclass.class.php
You may ask me, why not create a __construct()
method in sampleclass.class.php and just move the "$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);" there ?. well if I'm to do this, all other classes will have to have it's own constructor
and $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
declared there right?
With $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
on the connection.db.php
I assume that I can reuse it versatile-ly.
You see I've declared a public $db;
in sampleclass.class.php
but I don't know what's next. If someone can help me dearly, then I would be a lot grateful.
PS: I've already consulted Dr. Google Ph.D. regarding this matter, and I only got tons of articles or video tutorials showing how to use and do OOP PHP-MySQLi Programming but without using classes and methods.
You can use global in your case just incluce or require
connection.db.php
then useglobal
in php.Something like this might help you
As you're alread using objects, you probably should also go to a OOP (Object Oriented Programing) implementation.
So, you may put your
$db
variable as a static variable in the sampleclass.class.php file, as in:Like that you instantiate the mysqli object using this code:
Even better, you could create a method to instantiate the $db connection, initialize the variables in that class:
In index.php, you may get the db instance with the following code, not reconnecting every time:
You'd be best to create a
DB
class or harnessing an already created one to achieve what you're trying to do.The usual flow for things like this is call Lazy Loading/Dependency Injection. Where you're passing the required objects into the class.
If you choose this path, I suggest that you read up on Dependency Injection, as many things do, it has pros AND cons but is essential in OOP.
As Ben Stated in the comments:
A side not on the above mentioned, you'd be best to look at PHPTheRightWay, they list a lot of stuff, including Dependency Injection.
You'll end up creating something like. It'd be better if you followed this example to understand how it works:
You can construct this anyway you please. Or just make the
mysqli
object accessible, allowing you to call it.Now we get to the fun stuff. Actually injecting it into your class;
If you just want to share the resource, you could harness
global
, but it is strongly discouraged.If you chose this path, you'd be best to assign the
global
instance of$db
to an internal class variable, like$this->db
.