RE: [PHP] general OO php question

From: Date: Tue, 07 Oct 2003 00:26:09 +0000
Subject: RE: [PHP] general OO php question
Groups: php.general 
Request: Send a blank email to php-general+get-165257@lists.php.net to get a copy of this message
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.

« previous php.general (#165257) next »