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
Filed under: PHP, PHP Tutorials, PHP5 OOPS Tutorials
its very awesome,fantastic,amazing and …. and ……