Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'Autoload.php';
- class Event{
- static $event_action = array();
- static $subscribers = array();
- static $type = NULL;
- function __construct($event_type, $properties){
- self::$type = $event_type;
- if(!empty($properties) && gettype($properties)=='array'){
- foreach($properties as $property_name => $property_value){
- $this->{$property_name} = $property_value;
- }
- }
- }
- public function dispatch(){
- static::get_subscribers();
- static::notify();
- }
- static function subscribe($event_type, EventObserver $observer){
- if(empty(static::$event_action[$event_type])){
- static::$event_action[$event_type] = array();
- }
- static::$event_action[$event_type][] = $observer;
- }
- private static function get_subscribers(){
- static::$subscribers = static::$event_action[self::$type];
- }
- private static function notify(){
- if(!empty(static::$subscribers)){
- foreach(static::$subscribers as $subscriber){
- self::exec($subscriber);
- }
- }
- }
- static function exec($subscriber){
- var_dump($subscriber);
- $subscriber->action->__invoke();
- }
- }
- class EventObserver{
- public function __construct($action){
- if(!empty($action)){
- $this->action = $action;
- }
- }
- public function notify(){
- $this->action();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement