<?php
class Post
{
protected $text;
public function __construct($text)
{
$this->text = $text;
}
public function show()
{
printf('%s' . PHP_EOL, $this->text);
}
}
class SponsoredPost extends Post
{
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(Post $post)
{
$post->show();
}
foreach ($posts as $post) {
processPost($post);
}