Psr7 Http Message, why immutable?

前端 未结 1 485
清歌不尽
清歌不尽 2021-02-19 20:21

I am looking at the PSR-7 interfaces and thinking of a way on how to implement them.

I\'ve also been reading this blog post. Apparently the objects that implement the

相关标签:
1条回答
  • 2021-02-19 21:00

    I suggest you to read this document, where all the design choices are explained in detail.

    In particular you should read the Why value objects? and the New instances vs returning $this sections.

    The key points are the following:

    In essence, modeling HTTP messages as value objects ensures the integrity of the message state, and prevents the need for bi-directional dependencies, which can often go out-of-sync or lead to debugging or performance issues.

    and

    These operations can be accomplished with value objects as well, with a number of benefits:

    • The original request state can be stored for retrieval by any consumer.
    • A default response state can be created with default headers and/or message body.

    If you want to dig deeper, I'd recommend to look in the history of the fig mailing list (you can find it here), where there was a lot of discussion regarding the immutability of the objects

    0 讨论(0)
提交回复
热议问题