The attach() method is implementation specific for zend-diactoros, the PSR doesn't actually include that, thus the only standard way of replacing the stream resource is creating a new Stream, and that's consistent with how the rest of the PSR works.
I would suggest reading the post Michael Dowling wrote at the time [1], referred by Matthew during that discussion. I think the concerns about side-effects are addressed by the requirement of leaving the Stream instance unusable whenever the underlying resource is accessed. [1] http://mtdowling.com/blog/2014/07/03/a-case-for-higher-level-php-streams/ Il giorno gio 29 dic 2016 alle ore 14:16 Rasmus Schultz <[email protected]> ha scritto: > Reading through those comments, I am if anything now more baffled. > > Looking at an implementation of attach() such as zend-diactoros, the > implementation of attach() is precisely identical to that of __construct() > - it effectively permits you to replace the entire object with a different > object, e.g. provides a kind of mutability that enables you to replace the > entire object. > > As far as I can figure, the only thing you can hope to accomplish with > that, is side-effects - a degree of side-effects not even possible with > native streams. > > And we wanted to avoid side-effects, right? That's why the rest of the > model is immutable. > > Can you point me to a real-world example of attach() and detach() in use? > > > On Thursday, December 29, 2016 at 1:48:42 PM UTC+1, Stefano Torresi wrote: > > Hey Rasmus, > > The detach() method was introduced to allow a fine grained control over > the underlying resource of the Stream object, and it provides a > standardized way to do that. > You can find more insights in the original discussion here: > https://groups.google.com/d/msg/php-fig/CTPRa2XP8po/kYZnFxl4JjMJ > > Admittedly, the metadoc is a bit lacking in this regard. > > I think we could amend it with a clarifying errata, couldn't we? > > Il giorno gio 29 dic 2016 alle ore 13:25 Rasmus Schultz < > [email protected]> ha scritto: > > The detach() method of StreamInterface in PSR-7 isn't mentioned anywhere > in the documentation or meta. > > The only documentation for it, is the inline documentation, which doesn't > explain what it's for: > > > Separates any underlying resources from the stream. > > So basically, this sets the internal resource handle (an implementation > detail!) to null, right? > > > After the stream has been detached, the stream is in an unusable state. > > So if I want my stream object in an invalid, unusable state, this is the > method to call. > > Huh? > > When would I want that? > > Since streams are (primarily) intended as wrappers around PHP > stream-resource handles, I would have expected that the way to "detach" a > StreamInterface instance from the native stream-resource handle, is to > destroy the object - since a StreamInterface-wrapper around a PHP > stream-resource handle really represents only one thing: the > resource-handle. > > When would "detaching" and leaving the object in an invalid state make > more sense than simply destroying the object, and thereby the reference to > the stream-resource handle? > > -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > > > To view this discussion on the web visit > https://groups.google.com/d/msgid/php-fig/99fd9037-331d-4645-ad0f-c911ae370356%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/99fd9037-331d-4645-ad0f-c911ae370356%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/php-fig/08a6b4ab-fcec-4ccc-8b0f-351cdf3612cd%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/08a6b4ab-fcec-4ccc-8b0f-351cdf3612cd%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CAFojS1sWFu3OqqNuYf1zSsFiB1pVue2B9Ar7j65P5_UVnyxPCA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
