phasync\DeadmanSwitch
final
class
Documentation
A safety mechanism that triggers a callback when garbage collected.
This is useful for detecting when a coroutine terminates unexpectedly, allowing cleanup actions like marking a buffer as failed or closing a connection.
Usage:
phasync::go(function() use ($sb) {
$deadman = $sb->getDeadmanSwitch();
while ($data = fread($socket, 8192)) {
$sb->write($data);
}
$sb->end(); // Always end properly
// If coroutine exits without calling end(), $deadman triggers on GC
});
Properties (2)
private
?
Closure $callback
private
bool $triggered
Methods (5)
Create a new DeadmanSwitch.
Destructor triggers the callback if not already triggered or disarmed.
Manually trigger the switch, executing the callback.
Disarm the switch, preventing the callback from being executed.
Check if the switch has been triggered.