Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class model {
- private $arrValues = array();
- public function __get($var) {
- return $this->arrValues[$var];
- }
- public function __set($var, $val) {
- $this->arrValues[$var] = $val;
- }
- }
- class contrlr implements SplOberver, SplSubject {
- private $objModel;
- public function __construct( model $objModel ) {
- $this->objModel = $objModel;
- }
- public function parsePost( array $arrPost ) {
- foreach ( $arrPost as $key => $value ) {
- $this->objModel->$key = $value;
- }
- }
- public function update() {
- }
- public function attach() {
- }
- public function detach() {
- }
- }
- class view implements SplObserver {
- private $strHtml;
- public function getHtml() {
- return $this->strHtml;
- }
- }
- class Observer implements SplObserver {
- function update(SplSubject $subject) {
- echo "notified: " . $subject->status . "n";
- }
- }
- class Subject implements SplSubject {
- private $status;
- private $observer;
- function __construct(SplObserver $observer) {
- $this->attach($observer);
- }
- function __destruct() {
- $this->detach($this->observer);
- }
- function __get($prop) {
- return $this->$prop;
- }
- function __set($prop, $val) {
- $this->$prop = $val;
- $this->notify();
- }
- function notify() {
- $this->observer->update($this);
- }
- function attach(SplObserver $observer) {
- $this->observer = $observer;
- $this->__set("status", "attaching");
- }
- function detach(SplObserver $observer) {
- $this->__set("status", "detaching");
- $this->observer = null;
- }
- }
- $observer = new Observer();
- $sub1 = new Subject($observer);
- $sub1->status = "testing";
- $sub2 = new Subject($observer);
- unset($sub2);
- $sub1->status = "testing again";
- abstract class SubjectObserver {
- protected $observer_list = array(); // assoc array of classname=>object
- public $status;
- function update(SubjectObserver $subject) {
- echo get_class($this) . " notified by " . get_class($subject) . ": " . $subject->status . "n";
- }
- function __destruct() {
- foreach ($this->observer_list as $observer)
- { $this->detach($observer); }
- }
- function notify() {
- $args = func_get_args();
- foreach ($args as $arg)
- { $this->observer_list[get_class($arg)]->update($this); }
- }
- function attach() {
- $args = func_get_args();
- foreach ($args as $arg) {
- $this->observer_list[get_class($arg)] = $arg;
- $this->status = "attaching " . get_class($arg);
- $this->notify($arg);
- }
- }
- function detach() {
- $args = func_get_args();
- foreach ($args as $arg) {
- $this->status = "detaching " . get_class($arg);
- $this->notify($arg);
- unset($this->observer_list[get_class($arg)]);
- }
- }
- }
- class model extends SubjectObserver {}
- class view extends SubjectObserver {}
- class controller extends SubjectObserver {}
- // initialize
- $model = new model();
- $view = new view();
- $controller = new controller();
- $model->attach($controller, $view);
- $view->attach($controller, $model);
- $controller->attach($model, $view);
- // process some input
- $controller->status = "input received";
- $controller->notify($model);
- $model->status = "data filtered";
- $model->notify($controller, $view);
Add Comment
Please, Sign In to add comment