Challenge: Type Hinting

Write an interface and implement a method using type hinting.

We'll cover the following

Task

  • Create a User interface with setter and getter methods for the $username and $gender properties.

  • Now, create a Commentator class to implement the User interface.

  • Create a function to add Mr. or Mrs. 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 name Jane and against another user with the name Bob. 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 1400+ tech skills courses.