# Operating System

> Section in “Backward Incompatible Changes › Changed Semantics” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/backward-incompatible-changes/changed-semantics/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.

``` php
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`](https://packagist.org/packages/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:

- `dir_closedir(): bool`
- `dir_opendir(string $path, int $options): bool`
- `dir_readdir(): string`
- `dir_rewinddir(): bool`
- `mkdir(string $path, int $mode, int $options): bool`
- `rename(string $path_from, string $path_to): bool`
- `rmdir(string $path, int $options): bool`
- `stream_cast(int $cast_as): resource`
- `stream_close(): void`
- `stream_eof(): bool`
- `stream_flush(): bool`
- `stream_lock(int $operation): bool`
- `stream_metadata(string $path, int $option, mixed $value): bool`
- `stream_open(string $path, string $mode, int $options, string &$opened_path): bool`
- `stream_read(int $count): string`
- `stream_seek(int $offset, int $whence = SEEK_SET): bool`
- `stream_set_option(int $option, int $arg1, int $arg2): bool`
- `stream_stat(): array`
- `stream_tell(): int`
- `stream_truncate(int $new_size): bool`
- `stream_write(string $data): int`
- `unlink(string $path): bool`
- `url_stat(string $path , int $flags): array`

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.

---

Previous: [Graphics](https://php7explained.com/backward-incompatible-changes/changed-semantics/graphics/index.html.md) | Next: [Security](https://php7explained.com/backward-incompatible-changes/changed-semantics/security/index.html.md)
