Challenge: Interfaces

Write a class and make it implement multiple interfaces along with inheritance.

We'll cover the following

Task

  • Create a User class with a protected $username property and methods that can set and get the $username.
  • Create an Author interface with two abstract methods that can give the user an array of authorship privileges. The first method is setAuthorPrivileges() with the parameter $array, and the second method is getAuthorPrivileges().
  • Create an Editor interface with the method setEditorPrivileges() with the parameter $array, and another with the getEditorPrivileges() method to set and get the editor’s privileges.
  • Create an AuthorEditor class that extends the User class and implements both the Author and the Editor interfaces.
  • Add some methods in the AuthorEditor class that it should implement and the properties that these methods force us to add to the class.
  • In the test() method, create an object named $user1 from the class AuthorEditor and set its $username to Balthazar. Set an array of authorship privileges, including write text and add punctuation, in the $user1 object. Set the editorial privileges edit text and edit punctuation in the $user1 object.
  • Write code in the test() method to get and print the $user1 name and privileges.

Expected output

Balthazar has the following privileges:  write text, add punctuation, edit text, edit punctuation,

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.