Rofihimam

Untitled

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