Skip to content

Instantly share code, notes, and snippets.

@caramelchocolate
Created January 14, 2020 05:26
Show Gist options
  • Save caramelchocolate/96c0d0ca81a48cd3bbd9ead9cb52893f to your computer and use it in GitHub Desktop.
Save caramelchocolate/96c0d0ca81a48cd3bbd9ead9cb52893f to your computer and use it in GitHub Desktop.
Design Pattern: Before/After Pattern
<?php
# ref: http://hyuki.com/dp/dpinfo.html#BeforeAfter
abstract class Executor {
abstract protected function before();
abstract protected function execute();
abstract protected function after();
public function perform () {
$this->before();
try {
$this->execute();
} finally {
$this->after();
}
}
}
class Action extends Executor {
protected function before() {
echo 'before' . PHP_EOL;
}
protected function execute() {
echo 'execute' . PHP_EOL;
//throw new Exception('Error');
}
protected function after() {
echo 'after' . PHP_EOL;
}
}
$Action = new Action();
$Action->perform();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment