OOPs Tutorials
- What is OOPs
- Access Modifier
- Constructor
- Destructor
- Polymorphism
- Inheritence
- Method Overriding
- Abstract class
- Interface
- Traits
- This keyword
- static keyword
- Final keyword
- Class Constant
- Exception Handling
- Binding in php
- tostring in php
- Object Iteration
- Reflection
- Magic methods
- Namespaces
- CRUD Function
- OOPs CRUD
- CRUD Example
Important Links
What is this keyword?
$this variable is a pointer to the object making the function call. $this variable is not available in static methods.
File name : index.php
class Customer {
private $name;
public setName($name) {
$this->name = $name;
}
}
$c1 =new Customer();
$c2 =new Customer();
$c1->setName(“Stuart”);
$c2->setName(“Broad”);
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(“Stuart”), $this in the setName() function is a pointer or a reference to the $c1 variable.
File name : index.php