Giter Club home page Giter Club logo

php-eventdispatcher's Introduction

PHP Event Dispatcher

PHP Event Dispatcher is the most easy-to-use and simple Event Dispatcher PHP Class (Singleton Pattern).

The original idea is from Symfony's event listener system. http://symfony.com/doc/current/components/event_dispatcher/index.html

But I think it is a bit complex to me, I just want a simple Event Dispatcher for my project. Therefore I made this for myself, and someone else needs the simple EventDispatcher.

Installation

Download class.event.php to your project folder, and then include it anywhere.

include_once('class.event.php');

PHP Event Dispatcher is designed as Singleton Pattern, you can not call it by New EventDispatcher() Just because to make sure that only an Event Dispatcher to handle all events.

Use the following global functions to use PHP Event Dispatcher, you can put it anywhere and easily inject in any framework.

How to use

Add a listener function to an event

addListener('event_name', 'function_name');

Run this event

doDispatch('event_name');

Remove a listener function from an envet

Return: true / false

removeListener('event_name', 'function_name');

Check out if there is any listener defined by an event

Return: true / false

hasListener('event_name');

Check if an event Listener actually exists

Return: true / false

isListening('event_name');

Check current running event Listener

nowListener();

Examples

test.php

<?php

include_once('class.event.php');

addListener('event_send_message', 'send_message');

function send_message() {
    echo '<p>A message has been sent!!</p>';
    echo '<p>Current Running Event is called: <strong>' . nowListener() . '</strong></p>';
}

doDispatch('event_send_message');

$event_name = 'event_send_message';

if ( hasListener($event_name) ) {
    echo '<p>Listener <strong>' . $event_name . '</strong> is existed.<p>';
}
else {
  echo '<p>Listener <strong>' . $event_name . '</strong> is not existed.<p>';
}

?>

php-eventdispatcher's People

Contributors

greenjackal avatar terrylinooo avatar

Stargazers

Andrey Morozov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.