Challenge: Type Hinting
Write an interface and implement a method using type hinting.
Task
-
Create a
User
interface with setter and getter methods for the$username
and$gender
properties. -
Now, create a
Commentator
class to implement theUser
interface. -
Create a function to add
Mr.
orMrs.
to the username. When you’re writing the function, make sure to type hint it correctly so it can only get the types it is expected to. -
In the
test()
method, create a user with the nameJane
and against another user with the nameBob
. Use this function to return their names with an appropriate salutation.
Expected output
Mrs. Jane and Mr. Bob
Coding exercise
Since these problems are designed for your practice, try to solve them yourself first. If you get stuck, you can click the “Show Solution” button to see how the problem can be solved. Good luck!
Get hands-on with 1300+ tech skills courses.