944,184 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1581
  • PHP RSS
Oct 26th, 2005
0

Newb PHP question

Expand Post »
I am using somone's code as example because i dont really understand it.
This is a small part of the code which i am using as example.
Here is the code:
[PHP]
class MySQLDB
{
var $connection; //The MySQL database connection
var $num_active_users; //Number of active users viewing site
var $num_active_guests; //Number of active guests viewing site
var $num_members; //Number of signed-up users
/* Note: call getNumMembers() to access $num_members! */

/* Class constructor */
function MySQLDB(){
/* Make connection to database */
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

/**
* Only query database to find out number of members
* when getNumMembers() is called for the first time,
* until then, default value set.
*/
$this->num_members = -1;

if(TRACK_VISITORS){
/* Calculate number of users at site */
$this->calcNumActiveUsers();

/* Calculate number of guests at site */
$this->calcNumActiveGuests();
}
}
[/PHP]

Can anyone explain me what does $this->connection mean another words what does $this really do and how it works.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
ashneet is offline Offline
147 posts
since Jun 2005
Oct 26th, 2005
0

Re: Newb PHP question

$this->connection is a variable representing the mySQL connection.
you're assigning that connection to $this->connection, although, I would rename it
Reputation Points: 8
Solved Threads: 6
Posting Whiz in Training
extofer is offline Offline
239 posts
since Aug 2005
Oct 28th, 2005
0

Re: Newb PHP question

So its basically just renaming the connection to $this->connection.
Reputation Points: 10
Solved Threads: 1
Junior Poster
ashneet is offline Offline
147 posts
since Jun 2005
Oct 30th, 2005
0

Re: Newb PHP question

Quote ...
Can anyone explain me what does $this->connection mean another words what does $this really do and how it works.
In PHP, as with most object oriented languages, $this is a reference variable that allows access to members variables of the class $this is used in. For example:

[PHP]
class NumberClass {
var $number;

function printNumber() {
printf($this->number);
}
}
[/PHP]

Will print the value of that class's $number variable, whatever that may be for the particular object. Somewhere else in code you could do this:

[PHP]
$numberObject = new NumberClass();
$numberObject->number = 5;
$numberObject->printNumber();
[/PHP]

And the output would be 5. Hope that helps explain the $this variable a bit.

-Fredric
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Classes
Next Thread in PHP Forum Timeline: PHP image resize issue.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC