Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface EscapeStrategy {
- public function escape($param);
- }
- class HTMLEscapeStrategy implements EscapeStrategy {
- public function escape($param) {
- return htmlentities($param);
- }
- }
- class Post {
- private $params = [];
- /**
- * @var EscapeStrategy
- */
- private $escapeStrategy;
- public function __construct(array $params = array(), EscapeStrategy $escapeStrategy) {
- $this->params = $params;
- $this->escapeStrategy = $escapeStrategy;
- }
- public function isEmpty() {
- return count($this->params) <= 0;
- }
- public function __get($name) {
- return $this->escapeStrategy->escape($this->params[$name]);
- }
- }
- $post = new Post($_POST, new HTMLEscapeStrategy());
- ?>
- <form action="" method="POST">
- <input type="text" name="firstName" />
- <input type="submit" />
- </form>
- <?php if(!$post->isEmpty()): ?>
- <b><?= $post->firstName; ?></b>
- <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement