Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should Guzzle\Stream\Stream close the resource in __destruct(), even if it is not him who opens it? #682

Closed
drscre opened this issue May 28, 2014 · 3 comments

Comments

@drscre
Copy link
Contributor

drscre commented May 28, 2014

This is not an issue, just a little bit counterintutive:

$fp = fopen('myfile.txt', 'r');
$r = $client->post('http://httpbin.org/post', ['body' => $fp]);
fclose($fp); // <--- error (already closed)

Also, it's unclear if the stream will be closed in case some exception occurs in the middle of the ->post()

Theoretically, there can be cases where you don't want the stream to be automatically closed (even though i can't think of one right now :-) )

@mtdowling
Copy link
Member

A stream object will close the underlying stream resource when it's __destruct() method is called or when you call close() on it. It will not close in the event of an exception because it is referenced by the exception. When the reference count equals 0, then the stream will __destruct(). If you don't want the stream object to close the underlying stream resource, then you can use the detach() method of the stream to remove ownership of the underlying resource from the stream object.

@TheHett
Copy link

TheHett commented Apr 20, 2020

Close resource in __destruct - it is bad way.

php.net says:

Note:
Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error.

https://www.php.net/manual/en/language.oop5.decon.php

@mfn
Copy link

mfn commented Apr 20, 2020

Ah, I missed this issue when I opened mine a few months ago => exact same thing => #2400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants