Let's practice what we have just learned
Let's add 2 methods to represent the register and mail functionalities in the User class. These methods would echo a string as placeholder for their actual purpose.
This is the User class that we are going to use for this excercise. The hello() method echoes the first name of the user.
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
// The user $firstName property can be approached with the $this keyword.
public function hello()
{
echo "hello, " . $this -> firstName;
}
}
Add a register() method to the class that echoes the string " >> registered".
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
public function hello()
{
echo "hello, " . $this -> firstName;
}
// A method to register the user.
public function register()
{
echo " >> registered";
}
}
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code
Add a mail() method to the class that echoes the string " >> email sent".
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
public function hello()
{
echo "hello, " . $this -> firstName;
}
// A method to register the user.
public function register()
{
echo " >> registered";
}
// A method to send the welcome email.
public function mail()
{
echo " >> email sent";
}
}
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code
Add return $this to the hello() method so it can be chained to any other method in the class.
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
public function hello()
{
echo "hello, " . $this -> firstName;
return $this;
}
// A method to register the user.
public function register()
{
echo " >> registered";
}
// A method to send the welcome email.
public function mail()
{
echo " >> email sent";
}
}
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code
Add return $this to the register() method so it can also be chained.
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
public function hello()
{
echo "hello, " . $this -> firstName;
return $this;
}
// A method to register the user.
public function register()
{
echo " >> registered";
return $this;
}
// A method to send the welcome email.
public function mail()
{
echo " >> email sent";
}
}
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code
Create a new $user1 object with the first name of "Jane".
For this object, chain the methods in the following order: hello() -> register() -> mail()
Expected result:
hello, Jane >> registered >> email sent
class User {
// The class properties.
public $firstName;
// A method that says hello to the user $firstName.
public function hello()
{
echo "hello, " . $this -> firstName;
return $this;
}
// A method to register the user.
public function register()
{
echo " >> registered";
return $this;
}
// A method to send the welcome email.
public function mail()
{
echo " >> email sent";
}
}
$user1 = new User();
$user1 -> firstName = "Jane";
// Chain the methods hello then register then mail.
$user1 -> hello() -> register() -> mail();
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code
Note that each method we want to chain to should return the $this keyword in order to not break the chain.
So, the hello() and register() methods have to return the $this keyword,
but there is no need to return $this from the mail() method since it ends the chain.