php / php/doc-en

Closed resources throw TypeErrors as of PHP 8.0.0

Open
#1,581 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Category: Engine
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description

The following code:

<?php
$res = fopen("php://memory", "w+");
fclose($res);
var_dump(@fwrite($res, "\n"));

Resulted in this output:


Fatal error: Uncaught TypeError: fwrite(): supplied resource is not a valid stream resource in /tmp/bug-fwrite.php:4
Stack trace:
#0 /tmp/bug-fwrite.php(4): fwrite(Resource id php/php-src#5, '\n')
php/php-src#1 {main}
  thrown in /tmp/bug-fwrite.php on line 4

But I expected this output instead:

bool(false)

This closed-stream test works as expected on PHP 7.2.34 and 7.4.29.
No such change to the behaviour of the fwrite function has been announced or documented since PHP 7.

If a little context helps, I have a library ported to PHP in which a write error should throw a specific exception, where the PHP implementation checks for fwrite returning false and generates the exception. The associated unit test passes with PHP 7, and the same test discovered that TypeError very quickly after I downloaded and compiled PHP 8.1.6.

All three of those PHP versions involved behave as expected when the call to fclose is removed, all printing "int(1)".

PHP Version

PHP 8.1.6

Operating System

Linux Mint 20.1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied fwrite and fclose example on PHP 8.1.6, then compare its output with PHP 7.2.34 and 7.4.29 as described. The issue names no documentation file or test; determine where this behavior should be documented and whether the expected result is still valid. Done means the relevant documentation accurately reflects the confirmed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.