As of PHP 8.6.0, the way stream errors are reported can be changed for
every operation that uses a
stream context, by setting the
stream context options
error_mode, error_store
and error_handler.
Each error is described by a StreamError object,
whose code property
is a StreamErrorCode case.
An error is either terminating, such as a file that
cannot be opened, or non-terminating, in which case it
is reported but does not throw in StreamErrorMode::Exception
mode. A single operation may report several errors.
The error_mode option takes a
StreamErrorMode case and selects how errors are
reported:
StreamErrorMode::Error, the default, emits
diagnostics such as E_WARNING and
E_NOTICE.
StreamErrorMode::Exception throws a
StreamException if the operation
reported a terminating error. Non-terminating errors do not throw and
emit no diagnostics.
StreamErrorMode::Silent neither emits diagnostics
nor throws.
The error_store option takes a
StreamErrorStore case and selects which errors
are kept for stream_last_errors(). With the default,
StreamErrorStore::Auto, this depends on the error mode:
| Error mode | Errors stored with StreamErrorStore::Auto |
|---|---|
StreamErrorMode::Error |
None |
StreamErrorMode::Exception |
Non-terminating errors |
StreamErrorMode::Silent |
All errors |
The error_handler option is a
callable that receives the list of
StreamError objects of every operation that
reported errors, unless a
StreamException is thrown.
Hinweis:
stream_context_set_default() rejects these options. They must be set on a context created with stream_context_create() that is passed to the function, or that the stream was opened with.
See the stream context options for examples of each error mode and of an error handler.