Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2006, 9:22 PM   #1
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Am I on the right track with OOP?

I havent programmed with PHP in over a year and Im starting again, by jumping into OOP, something I never really used before... kinda confused me at first, working on a mysql class. Anyways, heres what I have for the connection, is this complete sloppyness? or acceptable?

testmysql.php
<?php
require_once "config.php";
require_once "classes/mysql.php";

$db = new mysqldb;
$db->dbhost = $dbhost;
$db->dbusername = $dbusername;
$db->dbpassword = $dbpassword;
$db->dbname = $dbname;

$conn = $db->connect();

if (!$conn) {
	echo 'Error Connecting to the database.';
}
else {
	echo 'Successful Connection.';
}
?>
(config.php contains $dbhost, username pass and name variables)

mysql.php
<?php
class mysqldb
{
var $dbhost;
var $dbusername;
var $dbpassword;
var $dbname;

	function connect() {
		$dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword);
			if (!$dblink) {
			}
			elseif (!mysql_select_db($this->dbname, $dblink)) {
				$dblink = false;
			}
		return $dblink;
	}
}
?>

I wrote the connect to select the database too because my scripts only using 1 database and it would be quite an annoyance to have to have an extra line in every file to select the database. How does it look?
brokenhope is offline   Reply With Quote
Old Dec 10th, 2006, 10:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
What is this? (rearranged for logical clarity)
if (!$dblink) 
{
   // Empty blocks are acceptable, but why?
}
elseif (!mysql_select_db($this->dbname, $dblink)) 
{
   $dblink = false;
}
That's like saying,
if ($dblink && !mysql_select_db ($this->dbname, $dblink)) $dblink = false;
Which would you rather come across, cold?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 11th, 2006, 4:55 PM   #3
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Alrighty, thanks, I see what your saying.

I have another question...

I have the mysqldb class, and now I have a validateuser class. The valideuserclass looks like this currently:

<?php
class validateduser extends mysqldb
{
	var $vusername;
	var $vpassword;

	function setvariables() {
		$this->vusername = $_SESSION['pkmnt_user'];
		$this->vpassword = $_SESSION['pkmnt_pass'];
	}

	function checkvalid() {
		$sel = "SELECT `id` FROM `members` WHERE username='$this->vusername' AND password='$this->vpassword' LIMIT 1";
		$sel = $this->query($sel);
		$num = $this->num($sel);
		if ($num != 1) {
			return false;
		}
		else {
			return true;
		}
	}
}
?>

Thats just a simple function to start off to see if the username and password's are valid. This class is right after the mysqldb class. The problem is when it extends the mysql class, all the vars in that class are dropped, as is the connection var, so all querys fail unless I fill out the connection again, and its just too sloppy to do that over and over again and start another connection, that is beyond out of the question. Is there any other way I can use the mysql classes functions within that, without extending it?
brokenhope is offline   Reply With Quote
Old Dec 13th, 2006, 2:01 AM   #4
headzoo
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 headzoo is on a distinguished road
Quote:
Originally Posted by brokenhope View Post
How does it look?
Right now your class is kind of pointless. But that's ok. If you're new to OOP, then you appear to be on the right track.

- Sean
headzoo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to keep track of bridge mappings. hoffmandirt Software Design and Algorithms 2 Sep 8th, 2006 11:19 AM
New Computer Lord Barlak Coder's Corner Lounge 73 Aug 7th, 2006 4:47 PM
Older members: How did you learn to code Mjordan2nd Coder's Corner Lounge 55 Mar 22nd, 2006 8:35 AM
YEY!! My First GUI To share with the world!! SaturN Python 8 Jul 16th, 2005 4:55 PM
Linked List Part 2 Nish C++ 0 Mar 4th, 2005 3:56 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC