hi friends.. i want to know that why we use $this-> keyword in classes of php.. and how to use this ...and when we should use it.. thanks in advance
Inside the code of a class, you can use the this to refer to the object itself. <?php class Foo { public $Bar; public function __construct() { $this->Bar = 0; } public function Baz() { $this->Bar = 10; } } $foo1 = new Foo(); $foo2 = new Foo(); $foo1->Baz(); // sets the value of $Bar inside $foo1 to 10 // does not affect the $Bar inside $foo2 var_dump($foo1, $foo2); ?> PHP:
It's how you access properties and methods within the same class. The scope of "$this->" extends only within the class or its children.