Created
October 30, 2017 14:35
-
-
Save tommelo/e9ca08d875e4739a5bff92b8b90bfe2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Events { | |
private static $listeners = array(); | |
public static function listen($event, $callback) { | |
self::$listeners[$event][] = $callback; | |
} | |
public static function trigger($event, $params) { | |
foreach (self::$listeners[$event] as $event => $callback) { | |
call_user_func($callback, $params); | |
} | |
} | |
} | |
class ButtonElement { | |
private $background; | |
private $text; | |
public function color($color) { | |
$this->background = $color; | |
Events::trigger('color', $color); | |
} | |
public function content($text) { | |
$this->text = $text; | |
Events::trigger('content', $text); | |
} | |
} | |
class ButtonElementObserver { | |
public function onColorChanged($color) { | |
echo "Color changed: " . $color; | |
} | |
public function onContentChanged($text) { | |
echo "Content changed: " . $text; | |
} | |
public function startObserver() { | |
Events::listen('color', array($this, 'onColorChanged')); | |
Events::listen('content', array($this, 'onContentChanged')); | |
} | |
} | |
$observer = new ButtonElementObserver(); | |
$observer->startObserver(); | |
$button = new ButtonElement(); | |
$button->color('blue'); | |
$button->content('save'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment