Skip to content

Instantly share code, notes, and snippets.

@phptuts
Created February 17, 2019 02:03
Show Gist options
  • Save phptuts/52a0c79f3619ff13b963765b23af30b4 to your computer and use it in GitHub Desktop.
Save phptuts/52a0c79f3619ff13b963765b23af30b4 to your computer and use it in GitHub Desktop.
Template Design Pattern
<?php
abstract class Gratitude {
protected $reporter;
protected $date;
public function __construct($reporter, $date)
{
$this->reporter = $reporter;
$this->date = $date;
}
final public function display()
{
echo 'REPORTER ' . $this->reporter . PHP_EOL;
echo 'MESSAGE ' . $this->customMessage();
echo 'DATE ' . $this->date . PHP_EOL;
}
public abstract function customMessage(): string;
}
class KindDeed extends Gratitude {
protected $deed;
public function __construct($reporter, $date, $deed)
{
parent::__construct($reporter, $date);
$this->reporter = $reporter;
}
public function customMessage(): string
{
return 'Good Deed ' . $this->deed . PHP_EOL;
}
}
class GreatNight extends Gratitude {
/**
* @var string
*/
protected $event;
public function __construct($reporter, $date, $event)
{
parent::__construct($reporter, $date);
$this->event = $event;
}
public function customMessage(): string
{
return ' Great night doing ' . $this->event . PHP_EOL;
}
}
(new GreatNight('Bill', '03/01/2019', 'Won bowling game'))->display();
(new KindDeed('Bill', '03/01/2019', 'Help some across street'))->display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment