Reputation: 165
[Edited/Updated]
Allow me to explain the situation. I have three files:
Below is db.class.php. When called, it connects to the DB in the constructor and closes the connection in the destructor. If you notice the query() method, it's simply a static INSERT at the moment, as that is where I'm stuck.
<?php
//
// Class: db.class.php
// Desc: Connects to a database and runs PDO queries via MySQL
// Settings:
error_reporting(E_ALL);
////////////////////////////////////////////////////////////
class Db
{
# Class properties
private $DBH; // Database Handle
private $STH; // Statement Handle
# Func: __construct()
# Desc: Connects to DB
public function __construct()
{
// Connection information
$host = 'localhost';
$dbname = 'removed';
$user = 'removed';
$pass = 'removed';
// Attempt DB connection
try
{
$this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Successfully connected to the database!';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
# Func: query(sql statement)
# Desc: Sends a query to the DB
public function query($sql_statement)
{
$sql = array(':color' => $sql_statement);
$this->STH = $this->DBH->prepare("INSERT INTO color_table (color) value ( :color )");
$this->STH->execute($sql);
}
# Func: __destruct()
# Desc: Disconnects from the DB
public function __destruct()
{
// Disconnect from DB
$this->DBH = null;
echo 'Successfully disconnected from the database!';
}
}
?>
Below is colors.class.php. For now, it only has an insert function. What I'm trying to do is essentially rip the what I have in the query() function from db.class.php and put it into the insertColor() function here, and pass the entire SQL statement whether it's an insert, delete, or update to the query() function.
<?php
//
// Class: colors.class.php
// Desc: Provides methods to create a query to insert,
// update, or delete a color from the database.
////////////////////////////////////////////////////////////
class Colors
{
# Class properties
protected $db;
# Func: __construct()
# Desc: Passes the Db object so we can send queries
public function __construct(Db $db)
{
$this->db = $db;
}
# Func: insertColor()
# Desc: Sends an INSERT querystring
public function insertColor($color)
{
$this->db->query($color);
echo 'Inserted color:' . $color;
}
}
?>
Below we have colors.php which is where everything above is instantiated and implemented. So here is where I'd pass what color I actually want to be inserted into the database.
<?php
Require('db.class.php');
Require('colors.class.php');
$db = new Db;
$colors = new Colors($db);
$colors->insertColor('TestColor'); // Passing the color here to be put into an insert statement
?>
Where I'm essentially stuck is I'm trying to make the colors.class.php create a PDO statement, and then pass it to the db.class.php query() method when they're ready to run. Right now, the PDO statement is simply defined in the query() method but I'm trying to avoid this. Essentially, I want to rip what I have out of the query() method and split it into three methods in the Colors class, one for insert, update, and delete.
However, I'm rather new to OOP programming and am having issues with all of the syntax, nor to I know if this is even a good approach. Any help is greatly appreciated, and let me know if more details or information is needed.
Upvotes: 1
Views: 4807
Reputation: 91734
I would remove the inclusion and initialization of the database from your class file and put it in colors.php
itself. Then you pass the $db
as an argument to the constructor:
<?php
Require('db.class.php');
Require('colors.class.php');
$db = new Db;
$colors = new Colors($db);
$colors->insertColor();
?>
I would also remove the default value in your constructor as your Colors
class will not work without the database:
public function __construct(Db $db) // constructor Colors class
Lastly I would look into autoloading of classes so that you don't have to require them manually.
Upvotes: 3
Reputation: 5093
Change $db->query;
to $db->query();
Also, $db will need to be declared as a global to access it like that. Might want to consider making it a private property of Colors
Upvotes: 0
Reputation: 6529
Yeah, because $db
is a local variable inside the method. Pass it as an argument:
class Colors
{
protected $db;
public function __construct(Db $db = null)
{
$this->db = $db;
}
public function insertColor()
{
if ($this->db != null)
{
$this->db->query();
}
}
}
And query
is a method so you have to add the brackets: $db->query()
Upvotes: 3