PracticeInterfaces - the next level of abstraction

Practice: Interfaces

In the "Interfaces – the next level of abstraction" tutorial, we learned how and when to use interfaces in our PHP code.

In the following exercise, we will be guided through the process of writing our own code which uses an interface to commit the classes that implement it to come up with concrete classes.

Coding exercise

In this tutorial, we saw that a class can implement more than one interface. In the concluding example, we will go one step further by letting the same child class inherit from both a parent class and from two interfaces.

Create a User class with a protected $username property and methods that can set and get the $username.

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Create an Author interface with the following abstract methods that can give the user an array of authorship privileges. The first method is setAuthorPrivileges(), and it gets a parameter of $array, and the second method is getAuthorPrivileges().

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Create an Editor interface with methods to set and get the editor's privileges.

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Create an AuthorEditor class that extends both the User class, and implements the Author and the Editor interfaces.

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Create in the AuthorEditor class the methods that it should implement, and the properties that these methods force us to add to the class.
For example, in order to implement the public method setAuthorPrivileges(), we must add to our class a property that holds the array of authorship privileges, and name it $authorPrivilegesArray accordingly.

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Now, let's create an object with the name of $user1 from the class AuthorEditor, and set its username to "Balthazar".

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Set in the $user1 object an array of authorship privileges, with the following privileges: "write text", "add punctuation".

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Set in the $user1 object an array with the following editorial privileges: "edit text", "edit punctuation".

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

Write the code to get the $user1 name and privileges:

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

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code
comments powered by Disqus