Jump to content

question about the use of the $this statement


silverglade

Recommended Posts

Hi, I do not know why we have to use the $this statement to refer to a variable within the same class.  Take this code for example.

 

class Car
{
    var $gas = 0;
    function addGas($amount)
    {
           $this->gas = $this->gas + $amount;
           echo "$amount gallons added to gas tank";
     }

}

 

why do we need "$this"? why cant we just write "$gas+= $amount;". Any help greatly appreciated. thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.