PHP
Published in PHP
avatar
1 minute read

PHP 8.3: New stream_context_set_options function

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

For the instance

<?php
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 to remove overloaded function signatures (functions that support more than one signature), PHP 8.3 introduces a new function stream_context_set_options(note the last "s") that supports the second signature mentioned above.

Note:  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.    

stream_context_set_options function signature:

<?php
/**
 * @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 {
        
    }

 It is possible to easily implement the stream_context_set_options function in PHP 8.2 and older PHP versions.

<?php
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);
    }
}

Comments