1

[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.

7
  • isn't require(), like all functions and variables, case sensitive? Commented Jul 13, 2012 at 21:30
  • @ReyGonzales Functions are not case sensitive, although variables are. require is not actually a function, it's a language construct - and these are also case-insensitive. Commented Jul 13, 2012 at 21:32
  • ftr, require* or include* are not functions, rather they are statements. So they should be written as require 'db.class.php'; Commented Jul 13, 2012 at 21:33
  • @ReyGonzales PHP is case insensitive! Commented Jul 13, 2012 at 21:33
  • @Besnik Variable names are case-sensitive, as are (by default) constant names. Commented Jul 13, 2012 at 21:34

3 Answers 3

3

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()

Sign up to request clarification or add additional context in comments.

5 Comments

Recommending global as a solution to an OP tagged as OOP is not a good idea.
@MikePurcell You're right! I removed the first approach. It was just quick and dirty...
Closer, but no cigar. I updated my post with the problem I ran into. Thanks for the help so far everyone!
@Xenostar You have to pass your database instance to the new colors object:$colors = new Colors($db);
It appears that you've set $db as a __construct argument, which is null, because you didn't actually pass it when you constructed the object.
3

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.

Comments

0

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

1 Comment

@Besnik Fixed. Thanks for borrowing the rest of my answer and downvoting me so you can gain a few extra points though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.