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->?
$username = addslashes($username);
$q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
$result = mysql_query($q, $this->connection);
return (mysql_numrows($result) > 0);
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.
$q's scope is limited only to usernameTaken(). Variables that you access using $this have a larger scope (the entire object).