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






Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here