$this Keyword
PHP supports Object Oriented Programming.
Just like this keyword in JavaScript or some other programming languages, the $this keyword in PHP refers to the current object. It is only available inside the Class methods.
Take a look at this code:
The $this keyword is used when you want to change the value for the $model property.
Change the property value inside the class
To do so, we need to add a set_name() method and use $this keyword inside it.
model = $model;
}
}
$tesla = new Car();
$tesla->set_name("Tesla");
//print the object
print_r($tesla);
//Car Object ( [model] => Tesla )
?>
That’s not the only way to change the value for the object property though.
Change the property value outside of the class
To do so, we need to add a set_name() method and use $this keyword inside it.
name = "Tesla";
//print the object
print_r($tesla);
//Car Object ( [model] => Tesla )
?>
class examples instanceof object-oriented OOP this keyword