...

/

Solution Review: $this Keyword

Solution Review: $this Keyword

Let's look at the solution of the $this Keyword challenge.

We'll cover the following...

Solution

Press + to interact
<?php
class User
{
public $firstName;
public $lastName;
public function hello() {
return "hello, " . $this -> firstName;
}
}
function test()
{
$user1 = new User();
$user1 -> firstName = 'Jonnie';
$user1 -> lastName = 'Roe';
return $user1 -> hello();
}
echo test();
?>

Explanation

  • Line 2: We write the User class with public properties as $firstName and $lastName.
  • Line 7: We add the hello() method to the User class. This method uses the $this to approach the $firstName property of the class and returns the message hello, firstName. Here, firstName will be
...