File: wildcard-listeners.md

package info (click to toggle)
php-zend-eventmanager 3.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: php: 3,542; xml: 696; makefile: 17
file content (42 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Wildcard Listeners

Sometimes you'll want to attach the same listener to all events of a given
instance — or potentially, with a shared event collection, all contexts.

## Attaching using the wildcard

To attach to all events on a given `EventManager` instance, you can use the
wildcard event, `*`:

```php
$events = new EventManager();
$events->attach('*', $listener);
```

Note that if you specify a priority, that priority will be used for this
listener for any event triggered.

What the above specifies is that **any** event triggered by the event manager
instance will result in notification of this particular listener.

## Attaching using the wildcard via a SharedEventManager

Using the `SharedEventManager`, you can indicate that you want to attach to all
events of a given identifier, a single named event across all identifiers, or
all events on all identifiers.

```php
$sharedEvents = new SharedEventManager();

// Attach to all events on the context "foo"
$sharedEvents->attach('foo', '*', $listener);

// Attach to the "foo" event of any context:
$sharedEvents->attach('*', 'foo', $listener);

// Attach to all events on all contexts:
$sharedEvents->attach('*', '*', $listener);
```

Note that if you specify a priority, that priority will be used for all events
specified.