SHARE
TWEET

Untitled

hamjoricantiq Feb 24th, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top