oihana-php-signals
A fast and flexible signal/slot implementation for event-driven programming.
📚 Documentation
Full project documentation is available at:
https://bcommebois.github.io/oihana-php-signals
🔗 Project on GitHub
View the full source code and contribute on GitHub:
https://github.com/BcommeBois/oihana-php-signals
📦 Installation
Requires PHP 8.4+. Install via Composer:
composer require oihana/php-signals
✨ 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
🚀 Quick start
use oihana\signals\Signal;
use oihana\signals\Receiver;
// Define a Receiver class
class NotificationHandler implements Receiver
{
public function receive( mixed ...$values ) :void
{
echo 'Notification: ' . implode(', ', $values) . PHP_EOL;
}
}
// Create receivers
$logger = function( mixed ...$values )
{
echo 'Log: ' . implode(', ', $values) . PHP_EOL;
};
$handler = new NotificationHandler();
// Setup signal
$signal = new Signal();
// Connect with different priorities
$signal->connect( $logger , priority: 10 ); // Executes first
$signal->connect( $handler , priority: 5 ); // Executes second
// Emit values to all connected receivers
$signal->emit( 'User logged in', 'user123' );
// One-time listener
$signal->connect(
fn() => echo 'First emit only!' . PHP_EOL,
autoDisconnect: true
);
🧰 Usage
Advanced usage with priority and auto-disconnect:
$signal = new Signal();
// High priority handler (executes first)
$signal->connect(
fn($msg) => echo "URGENT: $msg" . PHP_EOL,
priority: 100
);
// One-time handler (disconnects after first emit)
$signal->connect(
fn($msg) => echo "Initialization: $msg" . PHP_EOL,
priority: 50,
autoDisconnect: true
);
// Normal priority handler
$signal->connect(
fn($msg) => echo "Info: $msg" . PHP_EOL
);
// First emit - all three handlers execute
$signal->emit('System started');
// Second emit - only two handlers execute (auto-disconnect removed one)
$signal->emit('Processing data');
Note: WeakReferences are used for object receivers to allow proper garbage collection without preventing objects from being destroyed.
✅ Running Unit Tests
Run all unit tests:
composer test
Run a specific test file:
composer test tests/oihana/signals/SignalTest.php
🗒️ Changelog
See CHANGELOG.md for notable changes.
📄 License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
🔗 Related packages
oihana/php-core– core helpers and utilities used by this library: https://github.com/BcommeBois/oihana-php-coreoihana/php-reflect– reflection and hydration utilities: https://github.com/BcommeBois/oihana-php-reflect
👤 About the author
- Author: Marc ALCARAZ (aka eKameleon)
- Website: www.ooop.fr
- Email: marc@ooop.fr