<?php
abstract class BasePost
{
protected $text;
public function __construct($text)
{
$this->text = $text;
}
abstract public function show();
}
class Post extends BasePost
{
public function show()
{
printf('%s' . PHP_EOL, $this->text);
}
}
class SponsoredPost extends BasePost
{
private $sponsor;
public function __construct($text, $sponsor)
{
parent::__construct($text);
$this->sponsor = $sponsor;
}
public function show()
{
printf('%s by %s' . PHP_EOL, $this->text, $this->sponsor);
}
}
$posts = [];
$posts[0] = new Post('hello');
$posts[1] = new Post('hello again');
$posts[2] = new SponsoredPost('hello hello', 'dotinstall');
function processPost(BasePost $post)
{
$post->show();
}
foreach ($posts as $post) {
processPost($post);
}