Page 1 of 1

I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 12:14 pm
by Soheil
Hi everybody,

I'm a new one to this community and I'm looking for your help :)

I've just written my first PHP class and I really appreciate if you let me know your ideas about it.
Actually I wanna know if I've met the aspects of OOP or not.

This is the code of my class:

(One has to assume that I've connected to the database before initiating an object of this class)

Code: Select all

 
<?php
 
class Register
{
 
    private $uName;
    private $pass;
    private $cpass;
    private $mpass;
    private $email;
    private $regTime;
    private $membersTable;
    
    public function GetUserData($uName, $pass, $cpass, $email, $regTime)
    {
        $this->uName = $uName;
        $this->pass = $pass;
        $this->cpass = $cpass;
        $this->email = $email;
        $this->regTime = $regTime;
    }
    
    public function CheckEntries()
    {
        if(empty($this->uName))
        {
            return(false);
        }
        
        if(empty($this->pass))
        {
            return(false);
        }
        
        if(empty($this->email))
        {
            return(false);
        }
        
        return(true);
    }
    
    public function CheckPassword()
    {
        if($this->pass != $this->cpass)
        {
            return(false);
        }
        
        return(true);
    }
 
    public function GetMembersTable($membersTable)
    {
        $this->membersTable = $membersTable;
    }
    
    public function CheckUserExists()
    {
        $sql =  "SELECT uname FROM ". $this->membersTable ." where uname ='$this->uName'";
        $result = mysql_query($sql);
        $num = mysql_numrows($result);
        
        if ($num > 0) {
            return(true);
        }
        
        return(false);
    }
    
    private function HashPassword()
    {
        $this->mpass = md5($this->pass);
    }
    
    private function SetRegTime()
    {
        $this->regTime = time();
    }
    
    public function AddMember()
    {
        $sql = "INSERT INTO ". $this->membersTable ." (uname, pass, email, regtime) VALUES ('$this->uName', '$this->mpass', '$this->email', '$this->regTime')";
        $result = mysql_query($sql);
        
        if(!$result)
        {
            return(false);
        }
        
        return(true);
    }
}
 
?>
 
Thanks in advance.

Re: I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 1:23 pm
by jackpf
I think it could be improved.

For example, the CheckEntries() function. It has to be explicitly called to check the entries...whereas if you included it in the GetUserData() function, your class would automatically handle any empty data.

Also, maybe it's your preference to code like that...but the whole CheckEntries() function could be squashed into one if clause, using or (||) statements.

As well, you're selecting uname in the query. Since you're not retrieving any rows, you don't need to select anything.

Whether what you're actually doing requires a class may be debateable...I'd probably just put the whole thing into one function.

However, the useful thing about using a class for stuff like this, is that you can easily cache results. For example, if you're calling the CheckUserExists() function multiple times, possibly on the same user, it'd be more efficient to cache the results of the last query and return that for next time, rather than running the whole query instead.

The difference may not be noticeable now, but if you check out my forum (http://jackpf.co.uk/index.php?action=forum&forum=2009), you see each user has their own username colour, that's retrieved from the database. I obviously cache the colours, since they're not going to change during the duration of the script, and if I didn't, I'd have to run an obscene amount of queries...

So, using a class, if you have a private member $cache = array(); or something, each time you run the query, push the results into the array. Then, at the top of the CheckUserExists() function, you can check if the current user's query has already been cached. If so, just return that.

If you need to clear the cache or something, you can just call a new instance of the class, resulting in a clean array. Or if for some reason you need the same class, you can do it manually, like: $object->cache = array();

Anyway, just a few suggestions :)
Make of them what you wish.

Regards,
Jack.

Re: I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 1:41 pm
by Soheil
Dear Jack,
It helped me a lot.
I'm gonna try to solve the problems which you mentioned.
Thank you so much.

By the way, for someone like me who is a newbie in OOP programming but also has a relatively solid knowledge about PHP programming, which simple application do you recommend to write with the OOP view?
Actually I was trying to write a membership system.

Thanks.

Re: I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 1:52 pm
by jackpf
Cool, no problem :)

Well, tbh the first thing I made when I was learning OO PHP, was a mysql class. It's just a basic class that allows me to make sure I only have one open connection to the mysql server, and that allows me to count how many queries are being run on each page etc.

Another benefit is that if for some reason I wanted to change my SQL server to like...SQLite or something, all I need to change is the class...I don't need to trek through my code and change all the functions etc...

But yeah, I'd recommend trying that. Up to you though :)

Regards,
Jack.

Re: I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 1:58 pm
by Darhazer
Actually your Get* methods are Setters, so you have to rename them to Set*

Re: I need your ideas about my first PHP class

Posted: Sat Aug 22, 2009 2:12 pm
by Soheil
Thanks again Jack.
I'm gonna try that one.

Thank you Darhazer.