https://github.com/studio24/pubsub
Simple implementation of the PubSub design pattern in PHP
https://github.com/studio24/pubsub
Last synced: 2 months ago
JSON representation
Simple implementation of the PubSub design pattern in PHP
- Host: GitHub
- URL: https://github.com/studio24/pubsub
- Owner: studio24
- License: mit
- Created: 2016-03-28T14:33:02.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-16T22:18:26.000Z (almost 10 years ago)
- Last Synced: 2026-03-05T05:06:00.165Z (3 months ago)
- Language: PHP
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PubSub
Simple implementation of the PubSub design pattern in PHP.
## Installation
```sh
composer require studio24/pubsub
```
## Usage
Import at the top of your PHP script via:
```sh
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.
```sh
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.
```sh
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](LICENSE.md) for more information.
## Credits
- [Simon R Jones](https://github.com/simonrjones)