Newb PHP question

Reply

Join Date: Jun 2005
Posts: 147
Reputation: ashneet is an unknown quantity at this point 
Solved Threads: 1
ashneet's Avatar
ashneet ashneet is offline Offline
Junior Poster

Newb PHP question

 
0
  #1
Oct 26th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 239
Reputation: extofer is an unknown quantity at this point 
Solved Threads: 5
extofer's Avatar
extofer extofer is offline Offline
Posting Whiz in Training

Re: Newb PHP question

 
0
  #2
Oct 26th, 2005
$this->connection is a variable representing the mySQL connection.
you're assigning that connection to $this->connection, although, I would rename it
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 147
Reputation: ashneet is an unknown quantity at this point 
Solved Threads: 1
ashneet's Avatar
ashneet ashneet is offline Offline
Junior Poster

Re: Newb PHP question

 
0
  #3
Oct 28th, 2005
So its basically just renaming the connection to $this->connection.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Newb PHP question

 
0
  #4
Oct 30th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC