Forum Moderators: coopster

Message Too Old, No Replies

OOP newbie question - passing database object

         

gnetcon

8:40 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



Hello, all!

I've been coding in PHP for several years now, and I come from a procedural background. I saw the need for functions and all, but never really saw the need to learn OOP. Now I do.

I've since built a few small classes, but now I'm looking to build some bigger classes that need to use the database. I have been using PEAR::MDB2 for awhile now.

My question: what is the best way to use a current MDB2 connection inside of your class? I would rather use a current connection (I think) then update my classes to have their own db connection methods. Or is that wrong thinking? Do I pass a reference to it when I create a new object?

Anyway, if you're an OOP expert, I could use some advice before continuing.

TIA!

willis1480

9:00 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



Im no expert and nub to OOP as well, so take this for what it is worth. Most classes that Ive seen make the db connection as you always would, then access that connection via a global variable.

That is what I have seen from some opensource work ive done. Readup on the zenframework. Im sure whatever they are doing is prolly as good as it gets.

gnetcon

9:36 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



Thanks for the quick reply!

So I'm guessing I would do something similar?


class testClass {
...
function testMethod() {
global $dbConn; // MDB2 database object
$query = $dbConn->query("SELECT * FROM table");
...
}
}

For each method I would need my db connection, I would simply do the global call?

Thanks again!

RonPK

10:28 pm on Nov 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No OOP expert here, but globaling the database object seems ugly to me. From what I've read the way to go is as follows:

  1. make sure your database object is a singleton [nl.php.net], which means that at any time only one instance of it can exist.
  2. in a startup script, get an instance of the class and create the connection.
  3. inside any class you can use the object using the double colon notation, eg
    Database::query("SELECT * FROM table");
    ("Database" should be the name of your db class).

[edited by: RonPK at 10:32 pm (utc) on Nov. 17, 2008]

willis1480

10:35 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



um, not quite what I meant.

class mydb{
function connect($host, $pass, $etc..){
//i would want to make this link global, but this
//is normal connection
$this->link = @mysql_connect($host, $pass, etc...);
return true.
}
function my_query($sql){
$result = mysql_query($sql,$this->link);
return $result;
}
}

in my php file:
mydbObj = new mydb;
mydbObj->connect($host, $pass, $ect);
$result = mydbObj->my_query($sql);

now a connection has been made to db, and we have preformed a query. I think the connection is available, so instead of $result line, we could just do this:

$result = mysql_query($query);

not sure though. Anyway I hope this helps. There are a lot of people who have written different db classes. Dont know much about it though

willis1480

10:39 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



to add to Ron...
yes, you would want to check for a connection prior to establishing one. This would be part of the connect method within your database class.

gnetcon

3:20 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



OK, so my way was ugly. Reflects my looks, I suppose. : )

So, if I've already included MDB2.php and have created an instance of the class before this new class is called, I could simply do MDB2::query() in my code?

Thanks again for the help.

cameraman

4:23 pm on Nov 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using singleton looks about as ugly to me as the global. Why not just hold a reference to your global db object in your class and pass the object in the class's constructor:
class test {
protected $db;
public function __construct(&$dbobj) {
$this ->db = &$dbobj;
}
public function tables() {
$this->db->doqry("SHOW TABLES FROM db_name");
while($dat = $this->db->doqry())
$rtn[] = $dat;
return($rtn);
}// End tables()
};
$adb = new somedbclass('username','password','main');
$t = new test($adb);
$tables = $t->tables();

gnetcon

4:33 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



No idea, cameraman. : )

Again, OOP is new to me and I'm looking for the best way to do it, so I learn the right way from the beginning. My old way of just using functions I have done it both ways. I was just wondering from an OOP perspective the best way to use objects from others classes in your code.

Thanks again to all!