Example 1 — Attach a Notification Callback to a Context
Create a context, set a notification closure, then read it back with stream_context_get_params().
<?php
$log = [];
$ctx = stream_context_create();
$notifier = function (
int $code,
int $severity,
?string $message,
int $message_code,
int $bytes_transferred,
int $bytes_max
) use (&$log): void {
$log[] = $code;
};
stream_context_set_params($ctx, ["notification" => $notifier]);
$params = stream_context_get_params($ctx);
echo isset($params["notification"]) ? "notification set" : "missing", PHP_EOL;
echo "events so far: ", count($log), PHP_EOL; How It Works
- stream_context_create() builds an empty context.
- A closure matching the six-parameter signature is stored in $notifier.
- stream_context_set_params() attaches it under the notification key.
- No transfer ran, so the log is still empty, but get_params shows the callback is registered.

