Search
Write a publication
Pull to refresh

PHP сказ про то, как некромант инквизитора обманул

Reading time2 min
Views443
Как говорится: в сказке ложь, да в ней намек.

Все делалось не процесса ради а под конкретные нужды. Надеюсь шаблон окажется полезным. Код шаблона под катом.


  1. <?php
  2. /**
  3. * Cloak is a good thing when we need to hide some public methods and properties.
  4. */
  5. abstract class cloak {
  6.  protected $__cloak_method = array();
  7.  protected $__cloak_prop = array();
  8.  
  9.  final function __call($name, $args) {
  10.     if (isset($this->__cloak_method[$name]))
  11.      $this->__cloak_method[$name]();
  12.  }
  13.  
  14.  final function __set($name, $value) {
  15.     if (isset($this->__cloak_prop[$name]))
  16.      $this->__cloak_prop[$name] = $value;
  17.     else
  18.      $this->{$name} = $value;
  19.  }
  20.  
  21.  final function __get($name) {
  22.     if (isset($this->__cloak_prop[$name]))
  23.      return $this->__cloak_prop[$name];
  24.     else
  25.      return $this->{$name};
  26.  }
  27. }
  28.  
  29. class necromancer extends cloak {
  30.  function __construct() {
  31.     $this->__cloak_method['zombie_invocation'] = function() {
  32.      print 'Dugonas aldere simopa';
  33.     };
  34.     
  35.     $this->__cloak_prop['inventory'] = array('Kosparov\'s head', 'Rusty knife', 'Necromancer\'s staff');
  36.     $this->__cloak_prop['character'] = 'necromancer';
  37.  }
  38. }
  39.  
  40. class inquisitor {
  41.  function inspect_person($person) {
  42.     /**
  43.     * I am going to inspect this heretic.
  44.     */
  45.     $prop = get_object_vars($GLOBALS[$person]);
  46.     if (isset($prop['character']) && in_array($prop['character'], array('necromancer', 'witch')))
  47.      $this->kill($person);
  48.  }
  49.  
  50.  function kill($person) {
  51.     print 'Die, ' . $GLOBALS[$person]->character . "!\n";
  52.     unset($GLOBALS[$person]);
  53.  }
  54. }
  55.  
  56. class witch {
  57.  public $character = 'witch';
  58.  public function cast_damnation() {
  59.     print "Let your left hand to dry off!\n";
  60.  }
  61. }
  62.  
  63. ///////////////////////////////////////////////////////////////////////////////
  64. $inquisitor = new inquisitor();
  65. $necromancer = new necromancer();
  66. $witch = new witch();
  67.  
  68. $inquisitor->inspect_person('witch');
  69. $inquisitor->inspect_person('necromancer');
  70.  
  71. if(isset($witch))
  72.  $with->cast_damnation();
  73.  
  74. /**
  75. * Zombies' time!
  76. */
  77. $necromancer->zombie_invocation();
  78.  
  79. ?>
* This source code was highlighted with Source Code Highlighter.
Tags:
Hubs:
Total votes 22: ↑3 and ↓19-16
Comments10

Articles