Challenge: Inheritance

Try to implement the concept of inheritance.

We'll cover the following


  • Create a User class. Add a protected property named $username to the class and create a setter method that can set the value of the $username.
  • Create a class named Admin that inherits the User class.
  • Add a public method named expressYourRole() to the Admin class and make it return the class name.
  • Add another public method named sayHello() to the Admin class. This method should return the string Hello admin, XYZ with the $username instead of XYZ.
  • Inside the test() method, create an object $admin1 of the class Admin, set its name to Balthazar, and say hello to the user.

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 1200+ tech skills courses.