studio24/pubsub

Simple implementation of the PubSub design pattern in PHP

v1.0.2 2016-08-16 22:17 UTC

This package is auto-updated.

Last update: 2024-05-08 05:46:13 UTC


README

Simple implementation of the PubSub design pattern in PHP.

Installation

composer require studio24/pubsub

Usage

Import at the top of your PHP script via:

use Studio24\PubSub\PubSub;

Add a task to run at a certain event (subscribe)

This will run the passed anonymous function when the event 'myevent' is run.

PubSub::subscribe('myevent', function($name){
    // My code goes here
    echo $name;
});

PubSub::subscribe($event, $callback, $weight)

Params:

  • $event (string) Event name
  • $callback (callback) Callback function to run
  • $weight (int) Optional, weight to define the order subscribed tasks run, defaults to 10. The lower the number the earlier this callback runs

Run tasks at a certain event (publish)

This runs all tasks which are subscribed to the 'myevent' event, passing the argument $name.

PubSub::publish('myevent', $name);

PubSub::publish($event, ...$arguments)

Params:

  • $event (string) Event name
  • $arguments (mixed) Optional, one or many arguments to pass to the callback function

License

The MIT License (MIT). Please see License File for more information.

Credits