Using a class within a class?

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2010-03-29T15:09:21Z Indexed on 2010/03/29 15:13 UTC
Read the original article Hit count: 349

Filed under:
|
|
|

I built myself a MySQL class which I use in all my projects. I'm about to start a project that is heavily based on user accounts and I plan on building my own class for this aswell. The thing is, a lot of the methods in the user class will be MySQL queries and methods from the MySQL class.

For example, I have a method in my user class to update someone's password:

class user() {
    function updatePassword($usrName, $newPass)
    {
        $con = mysql_connect('db_host', 'db_user', 'db_pass');
        $sql = "UPDATE users SET password = '$newPass' WHERE username = '$userName'";
        $res = mysql_query($sql, $con);
        if($res) return true;
        mysql_close($con);
    }
}

(I kind of rushed this so excuse any syntax errors :) )

As you can see that would use MySQL to update a users password, but without using my MySQL class, is this correct? I see no way in which I can use my MySQL class within my users class without it seeming dirty. Do I just use it the normal way like $DB = new DB();? That would mean including my mysql.class.php somewhere too...

I'm quite confused about this, so any help would be appreciated, thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about class