SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: $this->

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this->

    So far I know that $this-> is used in object programming to access methods and properties of current object. But why $this-> is not used also for $q in the following code. When exactly do we need to put $this->?

    class MySQLDB
    {
    function usernameTaken($username){
    if(!get_magic_quotes_gpc()){
    $username = addslashes($username);
    }
    $q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
    $result = mysql_query($q, $this->connection);
    return (mysql_numrows($result) > 0);

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    usernameTaken is a public method, $q is local only to that function - it's not necessary to add another variable attached to the object when it can live in the function instead, and this way you can refer to the function multiple times without worrying about overriding, etc.
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $q's scope is limited only to usernameTaken(). Variables that you access using $this have a larger scope (the entire object).
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tnx, I understand now!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •