Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Am I on the right track with OOP? (http://www.programmingforums.org/showthread.php?t=12155)

brokenhope Dec 10th, 2006 10:22 PM

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?

DaWei Dec 10th, 2006 11:55 PM

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?

brokenhope Dec 11th, 2006 5:55 PM

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?

headzoo Dec 13th, 2006 3:01 AM

Quote:

Originally Posted by brokenhope (Post 121008)
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


All times are GMT -5. The time now is 1:37 AM.

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