...

/

Solution Review: Polymorphism

Solution Review: Polymorphism

Let's look at the solution of the Polymorphism challenge.

We'll cover the following...

Solution

Press + to interact
<?php
abstract class User {
protected $scores = 0;
protected $numberOfArticles = 0;
public function setNumberOfArticles($int) {
$numberOfArticles = (int)$int;
$this -> numberOfArticles = $numberOfArticles;
}
public function getNumberOfArticles() {
return $this -> numberOfArticles;
}
abstract public function calcScores();
}
class Author extends User {
public function calcScores() {
return $this -> scores = $this -> numberOfArticles * 10 + 20;
}
}
class Editor extends User {
public function calcScores() {
return $this -> scores = $this -> numberOfArticles * 6 + 15;
}
}
function test()
{
$author1 = new Author();
$author1 -> setNumberOfArticles(8);
$editor1 = new Editor();
$editor1 -> setNumberOfArticles(15);
return "Author score is " . $author1 -> calcScores(). " and Editor score is " . $editor1 -> calcScores();
}
echo test();
?>

Explanation

  • Line 2:
...