Created
November 9, 2016 18:48
-
-
Save gorkamu/7fb35daa09e7562ed31e39c8b1d7968a to your computer and use it in GitHub Desktop.
Ejemplo de interface
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 | |
| interface iTemplate | |
| { | |
| public function setVariable($name, $var); | |
| public function getHtml($template); | |
| } | |
| class Template implements iTemplate | |
| { | |
| private $vars = array(); | |
| public function setVariable($name, $var) { | |
| $this->vars[$name] = $var; | |
| } | |
| public function getHtml($template) { | |
| foreach($this->vars as $name => $value) { | |
| $template = str_replace('{' . $name . '}', $value, $template); | |
| } | |
| return $template; | |
| } | |
| } | |
| // Ésto no funcionará | |
| // Error fatal: La Clase BadTemplate contiene un método abstracto | |
| // y por lo tanto debe declararse como abstracta (iTemplate::getHtml) | |
| class BadTemplate implements iTemplate | |
| { | |
| private $vars = array(); | |
| public function setVariable($name, $var) { | |
| $this->vars[$name] = $var; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment