Guest User

Untitled

a guest
Dec 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. class model {
  2.  
  3. private $arrValues = array();
  4.  
  5. public function __get($var) {
  6. return $this->arrValues[$var];
  7. }
  8.  
  9. public function __set($var, $val) {
  10. $this->arrValues[$var] = $val;
  11. }
  12.  
  13. }
  14.  
  15. class contrlr implements SplOberver, SplSubject {
  16.  
  17. private $objModel;
  18.  
  19. public function __construct( model $objModel ) {
  20. $this->objModel = $objModel;
  21. }
  22.  
  23. public function parsePost( array $arrPost ) {
  24. foreach ( $arrPost as $key => $value ) {
  25. $this->objModel->$key = $value;
  26. }
  27. }
  28.  
  29. public function update() {
  30. }
  31.  
  32. public function attach() {
  33. }
  34.  
  35. public function detach() {
  36. }
  37.  
  38. }
  39.  
  40. class view implements SplObserver {
  41.  
  42. private $strHtml;
  43.  
  44. public function getHtml() {
  45. return $this->strHtml;
  46. }
  47. }
  48.  
  49. class Observer implements SplObserver {
  50.  
  51. function update(SplSubject $subject) {
  52. echo "notified: " . $subject->status . "n";
  53. }
  54.  
  55. }
  56.  
  57. class Subject implements SplSubject {
  58. private $status;
  59. private $observer;
  60.  
  61. function __construct(SplObserver $observer) {
  62. $this->attach($observer);
  63. }
  64.  
  65. function __destruct() {
  66. $this->detach($this->observer);
  67. }
  68.  
  69. function __get($prop) {
  70. return $this->$prop;
  71. }
  72.  
  73. function __set($prop, $val) {
  74. $this->$prop = $val;
  75. $this->notify();
  76. }
  77.  
  78. function notify() {
  79. $this->observer->update($this);
  80. }
  81.  
  82. function attach(SplObserver $observer) {
  83. $this->observer = $observer;
  84. $this->__set("status", "attaching");
  85. }
  86.  
  87. function detach(SplObserver $observer) {
  88. $this->__set("status", "detaching");
  89. $this->observer = null;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. $observer = new Observer();
  96. $sub1 = new Subject($observer);
  97. $sub1->status = "testing";
  98. $sub2 = new Subject($observer);
  99. unset($sub2);
  100. $sub1->status = "testing again";
  101.  
  102. abstract class SubjectObserver {
  103. protected $observer_list = array(); // assoc array of classname=>object
  104. public $status;
  105.  
  106. function update(SubjectObserver $subject) {
  107. echo get_class($this) . " notified by " . get_class($subject) . ": " . $subject->status . "n";
  108. }
  109.  
  110. function __destruct() {
  111. foreach ($this->observer_list as $observer)
  112. { $this->detach($observer); }
  113. }
  114.  
  115. function notify() {
  116. $args = func_get_args();
  117. foreach ($args as $arg)
  118. { $this->observer_list[get_class($arg)]->update($this); }
  119. }
  120.  
  121. function attach() {
  122. $args = func_get_args();
  123. foreach ($args as $arg) {
  124. $this->observer_list[get_class($arg)] = $arg;
  125. $this->status = "attaching " . get_class($arg);
  126. $this->notify($arg);
  127. }
  128. }
  129.  
  130. function detach() {
  131. $args = func_get_args();
  132. foreach ($args as $arg) {
  133. $this->status = "detaching " . get_class($arg);
  134. $this->notify($arg);
  135. unset($this->observer_list[get_class($arg)]);
  136. }
  137. }
  138.  
  139. }
  140.  
  141. class model extends SubjectObserver {}
  142.  
  143. class view extends SubjectObserver {}
  144.  
  145. class controller extends SubjectObserver {}
  146.  
  147. // initialize
  148. $model = new model();
  149. $view = new view();
  150. $controller = new controller();
  151.  
  152. $model->attach($controller, $view);
  153. $view->attach($controller, $model);
  154. $controller->attach($model, $view);
  155.  
  156.  
  157. // process some input
  158. $controller->status = "input received";
  159. $controller->notify($model);
  160. $model->status = "data filtered";
  161. $model->notify($controller, $view);
Add Comment
Please, Sign In to add comment