https://github.com/harryhope/messenger
A straightforward php publish/subscribe library.
https://github.com/harryhope/messenger
php pub-sub
Last synced: 3 months ago
JSON representation
A straightforward php publish/subscribe library.
- Host: GitHub
- URL: https://github.com/harryhope/messenger
- Owner: harryhope
- License: mit
- Created: 2014-06-06T05:38:29.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T00:10:28.000Z (over 6 years ago)
- Last Synced: 2025-03-13T10:35:34.786Z (10 months ago)
- Topics: php, pub-sub
- Language: PHP
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Messenger
=========
A straightforward php publish/subscribe library.
## Usage
Messenger lets you subscribe to functions and then trigger them when messages
are sent from other parts of your application.
```php
use harryhope\Messenger;
```
To subscribe:
```php
Messenger::on('name change', function($name) {
print 'Hello, ' . $name;
});
```
Named or anonymous functions can be used as subscriptions.
```php
$greeting = function($name) {
print 'How are you today, ' . $name;
};
Messenger::on('name change', $greeting);
```
Unsubscribe to a specific message + named callback combination, or to
all messages of a given message name by calling the off method without the
second parameter.
```php
// Remove one message + callback pairing
Messenger::off('name change', $greeting);
// Remove everything with the message 'name change'
Messenger::off('name change');
```
Use the send method to trigger associated subscriptions.
```php
Messenger::send('name change', 'Dave');
```
Messenger also allows for method chaining.
```php
Messenger::on('name change', $change_name)->on('day change', $change_day);
Messenger::send('name change', 'Dave')
->send('day change', 'Tuesday');
```