PHP5 Tutorial – $this variable explained
$this variable is a pointer to the object making the function call. $this variable is not avialable in static methods. We will learn more about static methods in the next series of tutorials.
Example:
class Customer { private $name; public setName($name) { $this->name = $name; } } $c1 = new Customer(); $c2 = new Customer(); $c1->setName(“Sunilâ€); $c2->setName(“Vishalâ€);
In the above example, $c1 and $c2 are two separate Customer objects. Each object has its own memory to store names. But if you see the function setName() is common. During run time, how can it make a difference as to which memory location to use to store the function values. Therefore, it uses the $this variable. As mentioned earlier, $this variable is a pointer to the object making a function call. Therefore when we execute $c1->setName(“Sunil”), $this in the setName() function is a pointer or a reference to the $c1 variable.
Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)
- PHP5 Tutorial – Learn to create a PHP5 Class
- PHP5 Tutorial – Learn to Create a PHP5 Class Object
- PHP5 Tutorial – Defining Attributes of a PHP5 Class
- PHP5 Tutorial – Defining Methods of a PHP5 Class
- PHP5 Tutorial – Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
- PHP5 Tutorial – $this variable explained
- PHP5 Tutorial – instanceOf Operator Explained
- PHP5 Tutorial – Magic Methods – __toString() method
- PHP5 Tutorial – Magic Methods – __get() and __set()
- PHP5 Tutorial – Magic Methods – __isset() and __unset()
- PHP5 Tutorial – Magic Methods – __call() method
- PHP5 Tutorial – Magic Methods – __autoload() method
- PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
- PHP5 Tutorial – Magic Methods – __clone() method

its very awesome,fantastic,amazing and …. and ……
nice tutorial. what is the explain of ->name? the letter after $this? and what if the function? are we can change ->name with another word? e.g. ->myname?
Thank You..
hi…i found this website quite helpful as it helped me in brushing up the basics…there was a gap of more than 8-11 months…it helped me to revise…it shud b done at bigger level….
Thank you Firoz for your comments.