Solution Review: Chaining Methods and Properties
Let's look at the solution of the Chaining Methods and Properties challenge.
We'll cover the following...
Solution
Press + to interact
<?phpclass User {public $firstName;public function hello() {echo "hello, " . $this -> firstName;return $this;}public function register() {echo " >> registered";return $this;}public function mail() {echo " >> email sent";}}function test(){$user1 = new User();$user1 -> firstName = "Jane";$user1 -> hello() -> register() -> mail();}test();?>
Explanation
- Line 2: We write the
User
class with$firstName
as a public property. - Line 5: We write the hello() method that says
hello
to the user. We return$this