Search⌘ K
AI Features

Solution Review: $this Keyword

Explore how to use the $this keyword in PHP to access class properties and chain methods within a class. Understand creating objects, setting public properties, and invoking methods that reference those properties to produce dynamic outputs. This lesson helps you master key object-oriented programming concepts crucial for effective PHP development.

We'll cover the following...

Solution

...
PHP
<?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

...