Re: general OO php question
| From: | Becoming Digital | Date: | Tue, 07 Oct 2003 04:48:02 +0000 |
| Subject: | Re: general OO php question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-165264@lists.php.net to get a copy of this message | ||
The "best" way to do this depends on what you really want to accomplish. Not knowing
anything about your project, it seems like your register function might fit within the constructs of
a larger class. However, it may do best as an independant object, but that's not really the
issue.
Regardless of how you choose to handle things, I wouldn't recommend coding in the
mysql_connect(). As Chris suggested, create a DB class for handling DB functions, or just use one
of the many that already exist. However, I would not recommend extending this class with Register,
as the two are independant entities. Rather, instantiate your DB class within Register.
Sorry for what may be confusing wording. I haven't slept in a few days.
Edward Dudlik
Becoming Digital
www.becomingdigital.com
----- Original Message -----
From: "Chris W. Parker" <cparker@swatgear.com>
To: "Kirk Babb" <kbcd@comcast.net>; <php-general@lists.php.net>
Sent: Monday, 06 October, 2003 20:26
Subject: RE: [PHP] general OO php question
Kirk Babb <mailto:kbcd@comcast.net>
on Monday, October 06, 2003 4:57 PM said:
> If I have a class called Registration, should I code in the
> mysql_connect inside each function inside the class that needs it, or
> should I make a function called dbConnect?
What you should do is define a db class and then extend that class with
the registration class.
(This is basically how I've been doing it. I'm no expert so it's very
possible this could be done in a more efficient way. Use at your own
risk. ;) )
(p.s. This is just pseudo-code)
Class Database
{
var $Result;
function Database()
{
// create usable db connection
}
function Query($sql)
{
// send a sql statement to the db
// store the results in $this->Result;
}
function GetResults()
{
// turn $this->Result into useable
// array and return it
}
}
Class Registration extends Database
{
function AddRegistrant($name, $age)
{
$sql = "INSERT INTO registrants\n"
." ( name\n"
." , age )\n"
."VALUES\n"
. ( $name\n"
. , $age )";
$this->query($sql);
}
function GetRegistrants()
{
$sql = "SELECT name, age FROM registrants";
$this->query($sql);
$rsRegistrants = $this->GetResults();
return $rsRegistrants;
}
}
HTH,
Chris.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php