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.
abstract class User {
protected $scores = 0;
protected $numberOfArticles = 0;
public function setNumberOfArticles($int)
{
// Cast to integer type
$numberOfArticles = (int)$int;
$this -> numberOfArticles = $numberOfArticles;
}
public function getNumberOfArticles()
{
return $this -> numberOfArticles;
}
}
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.
abstract class User {
protected $scores = 0;
protected $numberOfArticles = 0;
public function setNumberOfArticles($int)
{
// Cast to integer type
$numberOfArticles = (int)$int;
$this -> numberOfArticles = $numberOfArticles;
}
public function getNumberOfArticles()
{
return $this -> numberOfArticles;
}
// The abstract method.
abstract public function calcScores();
}
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
class Author extends User {
public function calcScores()
{
return $this -> scores = $this -> numberOfArticles * 10 + 20;
}
}
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
class Editor extends User {
public function calcScores()
{
return $this -> scores = $this -> numberOfArticles * 6 + 15;
}
}
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
$author1 = new Author();
$author1 -> setNumberOfArticles(8);
echo $author1 -> calcScores();
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
$editor1 = new Editor();
$editor1 -> setNumberOfArticles(15);
echo $editor1 -> calcScores();
Scratchpad to practice your coding *This will not be saved nor submitted to us.*
<?php
//Your practice code