Signal implements Signaler
A fast and flexible signal/slot implementation for event-driven programming.
This class provides a robust observer pattern implementation with priority-based execution, auto-disconnect capability, and support for both callable functions and Receiver objects. It is ideal for event-driven architectures and decoupled communication between components.
Features:
- Priority-based receiver execution (higher priority executes first)
- Auto-disconnect for one-time listeners
- Type-safe receiver management
- Efficient sorting and execution order
- Supports both object receivers implementing
Receiverand PHP callables
WeakReferences are used for object receivers to allow proper garbage collection without preventing objects from being destroyed.
Tags
Table of Contents
Interfaces
- Signaler
Constants
- RECEIVE = 'receive'
- The method name used when calling Receiver objects.
Properties
- $length : int
- The number of currently connected receivers.
- $throwable : bool
- Indicates whether exceptions from receivers are propagated.
- $receivers : array<string|int, SignalEntry>
- Internal storage for connected receivers.
Methods
- __construct() : mixed
- Creates a new Signal instance.
- connect() : bool
- Connects a receiver to this signal.
- connected() : bool
- Checks if any receivers are connected to this signal.
- disconnect() : bool
- Disconnects one or all receivers from this signal.
- emit() : void
- Emits a signal, calling all connected receivers with the provided values.
- hasReceiver() : bool
- Checks if a specific receiver is connected to this signal.
- toArray() : array<string|int, callable>
- Returns an array of all connected receivers.
Constants
RECEIVE
The method name used when calling Receiver objects.
public
mixed
RECEIVE
= 'receive'
Properties
$length read-only virtual
The number of currently connected receivers.
public
int
$length
Read-only property that returns the count of active receivers.
Tags
Hooks
public
int
get
$throwable
Indicates whether exceptions from receivers are propagated.
public
bool
$throwable
= true
$receivers
Internal storage for connected receivers.
protected
array<string|int, SignalEntry>
$receivers
Each element is a SignalEntry object containing the receiver callable, its priority, and auto-disconnect flag.
Methods
__construct()
Creates a new Signal instance.
public
__construct([array<string|int, callable|Receiver>|null $receivers = null ][, bool $throwable = true ]) : mixed
Parameters
- $receivers : array<string|int, callable|Receiver>|null = null
-
Optional array of initial receivers to connect. Each receiver will be connected with default priority (0).
- $throwable : bool = true
-
Indicates if the signal should throw exceptions when a receiver fails (default
true).
Tags
connect()
Connects a receiver to this signal.
public
connect(callable|Receiver $receiver[, int $priority = 0 ][, bool $autoDisconnect = false ]) : bool
Receivers are executed in order of priority (highest first). Duplicate receivers are ignored.
Parameters
- $receiver : callable|Receiver
-
The receiver to connect.
- $priority : int = 0
-
Execution priority (higher values execute first). Default: 0
- $autoDisconnect : bool = false
-
Auto-disconnect after first execution. Default: false
Tags
Return values
bool —True if successfully connected, false if duplicate or invalid receiver.
connected()
Checks if any receivers are connected to this signal.
public
connected() : bool
Tags
Return values
bool —True if at least one receiver is connected, false otherwise.
disconnect()
Disconnects one or all receivers from this signal.
public
disconnect([callable|Receiver|null $receiver = null ]) : bool
Parameters
- $receiver : callable|Receiver|null = null
-
The receiver to disconnect. Null disconnects all.
Tags
Return values
bool —True if disconnection occurred, false otherwise.
emit()
Emits a signal, calling all connected receivers with the provided values.
public
emit(mixed ...$values) : void
Receivers are called in priority order (highest to lowest). Receivers marked with autoDisconnect are automatically removed after being called.
Parameters
- $values : mixed
-
Zero or more values to pass to each receiver.
Tags
hasReceiver()
Checks if a specific receiver is connected to this signal.
public
hasReceiver(callable|Receiver $receiver) : bool
Parameters
- $receiver : callable|Receiver
-
The receiver to check.
Tags
Return values
bool —True if connected, false otherwise.
toArray()
Returns an array of all connected receivers.
public
toArray() : array<string|int, callable>
The returned array contains the actual callable references, not the SignalEntry wrapper objects.
Tags
Return values
array<string|int, callable> —Array of receiver callables in priority order.