PHP 8.3: New stream_context_set_options function

Version8.3
TypeNew Feature

PHP has a stream_context_set_option function that supports two function signatures. It can either accept an array of options to set for one or more contexts or wrappers, or it can accept a single wrapper name, option name, or its value.

function stream_context_set_option($stream_or_context, string $wrapper, string $option, mixed $value): bool {}

function stream_context_set_option($stream_or_context, array $options): bool {}

As part of PHP's efforts in removing overloaded function signatures (functions that support more than one signature), PHP 8.3 declares a new function stream_context_set_options (mind the last "s") that supports the second signature above.

In PHP 8.4, the stream_context_set_option($stream_or_context, string $wrapper, string $option, mixed $value) signature will be deprecated, and PHP 9.0 will remove the stream_context_set_option function.

New stream_context_set_options function

/**
 * @param resource $context The stream or context resource to apply the options to
 * @param array $options The options to set for `stream_or_context`
 * @return bool Returns true success or false on failure.
 */
function stream_context_set_options($context, array $options): bool {}

stream_context_set_options polyfill

It is possible to trivially implement the stream_context_set_options function in PHP 8.2 and older PHP versions:

if (\PHP_VERSION_ID < 80300) {
    /**
     * @param resource $context The stream or context resource to apply the options to
     * @param array $options The options to set for `stream_or_context`
     * @return bool Returns true success or false on failure.
     */
    function stream_context_set_options($context, array $options): bool {
        return \stream_context_set_option($context, $options);
    }
}

This polyfill merely declares a new stream_context_set_options function, and calls the existing stream_context_set_option function making use of the second signature it supports.

Backward Compatibility Impact

Unless a user-land PHP function named stream_context_set_options is declared, PHP applications should not have any compatibility issues with the introduction of this new function.

The new stream_context_set_options function can be polyfilled in older PHP versions.


RFC Discussion Implementation