Advertisement
hamjoricantiq

Untitled

Feb 24th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2.    
  3.     // Declare the interface 'iTemplate'
  4.     interface iTemplate {
  5.         public function setVariable($name, $var);
  6.         public function getHtml($template);
  7.     }
  8.  
  9.     // Implement the interface
  10.     // This will work
  11.     class Template implements iTemplate {
  12.         private $vars = array();
  13.  
  14.         public function setVariable($name, $var) {
  15.             $this->vars[$name] = $var;
  16.         }
  17.  
  18.         public function getHtml($template) {
  19.             foreach ($this->vars as $name => $value) {
  20.                 $template = str_replace('{'$name'}', $value, $template);
  21.             }
  22.             return $template;
  23.         }
  24.     }
  25.  
  26.     // This will not work
  27.     // Fatal error : Class BadTemplate contains 1 abstract methods
  28.     // and must therefore be declared abstract (iTemplate::getHtml)
  29.     class BadTemplate implements iTemplate {
  30.         private $vars = array();
  31.  
  32.         public function setVariable($name. $var) {
  33.             $this->vars[$name] = $var;
  34.         }
  35.     }
  36. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement