PracticeInheritance in object-oriented PHP

Practice: Inheritance in object-oriented PHP

The "Inheritance in object-oriented PHP" tutorial taught us how to use inheritance in order to reduce code duplication. For this aim, we extend the parent class by child classes that use the code from the parent, so we don't need to re-write the code repeatedly in each class. Instead, we need to write the code only once in the parent class and can use it in the child classes.

In the following section, we will be able to practice what we learned about inheritance in PHP by answering some questions and by writing our own code.

Answer the question

We learned about three access control modifiers (public, private, and protected) that we use to allow/restrict access to the code.
In the following table, we will use "V" to mark that the code can be accessed from a certain level, and "X" if it cannot be accessed.
Fill in the table with the right values.
For example, public code can be accessed from within the class, from the code within the child classes, and from the global scope, while private code cannot be accessed from the global scope (*).

ModifierClassChild classGlobal scope *
Public

 

 

 

Protected

 

Private

 

 

(*) The global scope is outside the classes.

Solution:

Coding exercise

In the following example, we will create an Admin class, which is a child class of the User class.

Create a user class.

Solution:

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

<?php
//Your practice code

Add to the class a private property with the name of $username.

Solution:

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

<?php
//Your practice code

Create a setter method that can set the value of the $username.

Solution:

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

<?php
//Your practice code

Create a class Admin that inherits the User class.

Solution:

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

<?php
//Your practice code

Now, let's add to the Admin class some code. First, add a public method by the name of expressYourRole(), and make it return the string: "Admin".

Solution:

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

<?php
//Your practice code

Add to the Admin class another public method, sayHello(), that returns the string "Hello admin, XXX" with the username instead of XXX.

Solution:

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

<?php
//Your practice code

Create an object $admin1 out of the class Admin
- set its name to "Balthazar" and say hello to the user.
Do you see any problem?

Expected result:
Notice: Undefined property: Admin::$username

Solution:

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

<?php
//Your practice code

What do you think is the cause of the problem?

Solution:

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

<?php
//Your practice code

Change the code to fix the problem.

Expected result:
Hello admin, Balthazar

Solution:

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

<?php
//Your practice code

Try to write the solution with a getter method inside the parent that can be used from the child class.

Expected result:
Hello admin, Balthazar

Solution:

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

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