Operating System

stream_socket_get_name() and IPv6

The stream_socket_get_name() function can be used to retrieve the name of local or remote sockets. As of PHP 7.3, it returns IPv6 addresses wrapped in brackets.

var_dump(
    stream_socket_get_name(
        fopen('http://localhost:8080/test', 'r'),
        false
    )
);

Executing the code shown above with PHP 7.3 and newer will print the output shown below:

string(11) "[::1]:35922"

Executing the code shown above with PHP 7.2 and older will print the output shown below:

string(9) "::1:35922"

Stream Wrappers

PHP uses a concept called streams to implement its file, network, data compression, etc. operations in a common and generalized way. You can think of a stream as a resource that can be linearly accessed for reading as well as writing and which allows seeking to an arbitrary location within the stream. PHP also supports so called stream wrappers to wrap streams for handling specific protocols or encodings, for instance. The mikey179/vfsstream package, for example, uses such a stream wrapper to implement a virtual file system that acts as a test double for the real file system in the context of unit tests.

A stream wrapper is implemented as a class that is then registered using the stream_wrapper_register() function to a protocol. Once registered, regular functions such as fopen() can be used to open a stream using the stream wrapper. As of PHP 7.4, such a stream wrapper class should implement the following methods:

In order to fix some bugs, the semantics of stream_read() and stream_write() methods were slightly changed in PHP 7.4 in that a return value of false is now interpreted as as failure. When no data is available and no error has occurred, then an empty string should now be returned.

If your stream wrapper class does not have a stream_set_option() method yet and you need PHP’s include, include_once, require, and require_once statements to work with your stream wrapper, then you need to add that method. A minimal implementation that just returns false is good enough.