note 127969 added to function.stream-notification-callback
| From: | aetonsi at main dot php dot net | Date: | Tue, 29 Nov 2022 15:51:36 +0000 |
| Subject: | note 127969 added to function.stream-notification-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219925@lists.php.net to get a copy of this message | ||
A couple of unreported behaviors:
- case 1) if this callback calls die/exit($msg), it will print $msg, then the execution will go on
until the request/wrapper is consumed, emitting a "PHP Warning: Failed to call user
notifier" on each invocation of the callback. After the last callback invocation, the script is
immediately terminated.
- case 2) if this callback throws an exception, it will behave the same way as exit/die, except for
the fact that after the last callback invocation it does not terminate the script. The exception is
instead raised in the scope of the request/wrapper, and can be caught with a try catch (right there
or at a higher level).
Example code for case 1). The final "TEST ECHO" string will NOT be printed.
<?php
$context = stream_context_create(['http' => ['ignore_errors' => true,]]);
stream_context_set_params($context, ['notification' => function () {
die('error');
}]);
file_get_contents('https://www.google.com',
false, $context);
echo "TEST ECHO";
?>
Example code for case 2). The exception is raised at the level of the file_get_contents call,
it's catched, and the final "TEST ECHO" gets printed.
<?php
$context = stream_context_create(['http' => ['ignore_errors' => true,]]);
stream_context_set_params($context, ['notification' => function () {
throw new Exception('...');
}]);
try{
file_get_contents('https://www.google.com', false, $context);
}catch(exception $e) { }
echo "TEST ECHO";
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 89.39.107.191)
----
Manual Page -- https://php.net/manual/en/function.stream-notification-callback.php
Edit -- https://main.php.net/note/edit/127969
Del: integrated -- https://main.php.net/note/delete/127969/integrated
Del: useless -- https://main.php.net/note/delete/127969/useless
Del: bad code -- https://main.php.net/note/delete/127969/bad+code
Del: spam -- https://main.php.net/note/delete/127969/spam
Del: non-english -- https://main.php.net/note/delete/127969/non-english
Del: in docs -- https://main.php.net/note/delete/127969/in+docs
Del: other reasons-- https://main.php.net/note/delete/127969
Reject -- https://main.php.net/note/reject/127969
Search -- https://main.php.net/manage/user-notes.php