$this keyword is used to access current properties and methods in a class. $this is used to point the current object of class in php.
We can also use $this to call one member function of a class inside another member function.
$this keyword is used to access non-static members of the class.
PHP Syntax:
$this->variable_name;
$this->method_name;
PHP Example :
<?php
// code by aryatechno!
class Tutorials
{
public $name;
function get_topic()
{
return $this->name;
}
function set_topic($name)
{
$this->name = $name;
}
}
$topic = new Tutorials();
$topic->set_topic("How to Learn this Keyword");
echo "Topic is ".$topic->get_topic();
?>
Output:
Topic is How to Learn this Keyword
Explaination : As above example, $this keyword is used to assign and retrive the value of $name variable of class Tutorials; $this keyword is used to point current object of class Tutorials. We can access all properties of class Tutorials using $this keyword inside class Tutorials.
Comments