PracticePolymorphism in PHP

Practice: Polymorphism in PHP

In the "Polymorphism principle in PHP" tutorial, we learned that we better give the same name to methods that do similar things in different classes.

In the practice exercise that accompanies the tutorial, we will enhance our knowledge by practicing the use of polymorphism in the PHP code that we are about to write.

Let's practice what we have just learned

* Press on the "solution button" to see our suggested solution.

Which of these sentences best defines the polymorphism principle in PHP?

  • A Methods that serve the same functionality in different classes should have the same name.
  • B The same function can have many names.
  • C Is the Greek term for inheritance.
  • D Can be implemented in most of the modern programming languages.
Solution:

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

<?php
//Your practice code

Coding example

In our coding example let's return to the User class that we used in the previous tutorials.

In order to implement the polymorphism principle, we are going to create an abstract User class that commits the classes that inherit from it to calculate the number of scores that a user has depending on the number of articles that he has authored or edited. On the basis of the User class, we are going to create the Author and Editor classes, and both will calculate the number of scores with the method calcScores(), although the calculated value will differ between the two classes.

This is the skeleton for the abstract User class:

abstract class User {
  protected $scores           = 0;
  protected $numberOfArticles = 0;
 
  // The abstract and concrete methods
}

Add to the User class concrete methods to set and get the number of articles:
 1.   setNumberOfArticles($int)
 2.  getNumberOfArticles()
$int stands for an integer.

Solution:

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

<?php
//Your practice code

Add to the class the abstract method: calcScores(), that performs the scores calculations separately for each class.

Solution:

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

<?php
//Your practice code

Create an Author class that inherits from the User class. In the Author create a concrete calcScores() method that returns the number of scores from the following calculation:
numberOfArticles * 10 + 20

Solution:

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

<?php
//Your practice code

Also create an Editor class that inherits from the User class. In the Editor create a concrete calcScores() method that returns the number of scores from the following calculation:
numberOfArticles * 6 + 15

Solution:

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

<?php
//Your practice code

Create an object, $author1, from the Author class, set the number of articles to 8, and echo the scores that the author gained.

Expected result:
100

Solution:

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

<?php
//Your practice code

Create another object, $editor1, from the Editor class, set the number of articles to 15, and echo the scores that the editor gained.

Expected result:
100
105

Solution:

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

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