GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2006, 04:24
abakws abakws is offline
New Member
 
Join Date: Oct 2006
Posts: 18
abakws is on a distinguished road

a quick question about using classes, objects and their properties...


Ok so here is my problem: I have the following code lines from a tutorial:
PHP Code:

<?
class math
{
    var $x=1;
    var $y=2;
     
     function sum()
     {
          return $this -> x + $this -> y;
     }
}

$s = new math;
echo "before changing x&y's values x+y = " . $s -> sum();
$s -> x=3;
$s -> y=4;
echo "after changing x&y's values x+y = " . $s -> sum();

?>

ok the code works perfectly but I dont understand something....

PHP Code:

$s -> x=3; 


Why so, and not
PHP Code:

$s -> $x=3; 


? In our class we declared them as "$x" respectively "$y" and what that "$this" means?

thanx in advance, very n00b in this...
  #2  
Old 20-Nov-2006, 09:31
nexuslite nexuslite is offline
New Member
 
Join Date: Nov 2006
Posts: 3
nexuslite is on a distinguished road

Re: a quick question about using classes, objects and their properties...


<?
// our class
class math
{
// our variables
var $x=1;
var $y=2;

// our functions
function sum()
{
// $this refers to our variables in this class
return $this->x + $this->y;
}
}

// declare a new class
$s = new math;

// show the total of x and y added together using the class function
echo "before changing x&y's values x+y = " . $s->sum();

// set class variables to new values
$s->x = 3;
$s->y = 4;

// show the total of x and y added together using the class function
echo "after changing x&y's values x+y = " . $s->sum();

?>
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.