Psr7 Http Message,为什么不可变?

Psr7 Http Message, why immutable?

我正在查看 PSR-7 接口并考虑如何实现它们。

我也一直在阅读 this blog post。显然,实现 PSR-7 接口的对象必须是不可变的。

因此,如果我从 MessageInterface 实现 withProtocolVersion 方法,那么它将看起来像这样:

public function withProtocolVersion($version)
{
    if ( $this->protocol === $version )
    {
        return $this;
    }

    $new = clone $this;
    $new->protocol = $version;
    return $new;
}

我的问题真的是,为什么不可变?为什么不简单地做一个return $this;

我并不是担心它分配的内存量,我只是真的看不出保持它不可变有任何好处。

正如博文所说,当您这样做时:

$request = $request
    ->withMethod('POST')
    ->withUrl(new Url('http://example.org/')
    ->withHeader('Content-Type', 'text/plain');

然后创建了四个副本,但是$request中的最终结果与我简单使用return $this时的结果相同,对吗?

为什么决定保持不变。那么为什么我必须做一个clone $this?它有什么好处?

我还没有真正理解这个想法。

我建议您阅读 this document,其中详细解释了所有设计选择。

您尤其应该阅读 Why value objects?New instances vs returning $this 部分。

重点如下:

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.

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.

如果你想深入挖掘,我建议你查看 fig 邮件列表的历史(你可以找到它 here),那里有很多关于对象