52: Handle Stream Flush and Close Failures
Overview
Section titled “Overview”Simulate a buffered write that fails during finalization. PHP 8.6 propagates stream flush and close failures through fclose(), file_put_contents(), and copy().
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. allow_url_fopen=1for the in-memory data URL used as the copy source.- Review the previous chapter if you’re following the series in order.
Allow about 15 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable stream-close-failures.php example.
Step 1: Run the example (~10 min)
Section titled “Step 1: Run the example (~10 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/52-stream-close-failures/stream-close-failures.php<?php
declare(strict_types=1);
final class BufferedSink{ public mixed $context;
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool { return true; }
public function stream_write(string $data): int { return strlen($data); }
public function stream_flush(): bool { return false; }}
if (!stream_wrapper_register('tutorialsink', BufferedSink::class)) { throw new RuntimeException('Cannot register test stream');}try { $stream = fopen('tutorialsink://output', 'w'); if ($stream === false) { throw new RuntimeException('Cannot open test stream'); } var_dump(fwrite($stream, 'sample')); var_dump(fclose($stream)); var_dump(file_put_contents('tutorialsink://output', 'sample')); var_dump(copy('data://text/plain,sample', 'tutorialsink://output'));} finally { stream_wrapper_unregister('tutorialsink');}Expected Result
Section titled “Expected Result”int(6)bool(false)bool(false)bool(false)Why It Works
Section titled “Why It Works”A successful fwrite() confirms the stream accepted bytes. You must also check finalization before reporting success. Check file_put_contents() with === false so a successful zero-byte result remains valid.
Exercise
Section titled “Exercise”Make the wrapper’s stream_flush() return true. All three operations should then report success, and file_put_contents() should report the number of accepted bytes.
Wrap-up
Section titled “Wrap-up”Continue with Handle New ValueErrors When Upgrading.