Created
January 14, 2020 05:26
-
-
Save caramelchocolate/96c0d0ca81a48cd3bbd9ead9cb52893f to your computer and use it in GitHub Desktop.
Design Pattern: Before/After Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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