When writing a gateway proxy request using php, why use clone if you want to change the request

when writing gateway proxy requests using php, why use clone, if you want to change the request, but clone can only make clone, for attributes. The object is a latent copy.
I know that what is defined in psr7 is that HTTP requests and responses must be regarded as unmodifiable, but because of the thought that clone is a deep copy of the properties of the object, but the object in the object is a latent copy, it is a bit puzzling.

Php
Mar.16,2021

PSR7 gives the corresponding interface withXXXX . As a user of the library, you don't need clone. For example, the implementation of guzzled has already helped you clone, and the attributes that need to be updated have also been updated for you:

.
public function withUri(UriInterface $uri, $preserveHost = false)
{
    if ($uri === $this->uri) {
        return $this;
    }
    $new = clone $this;
    $new->uri = $uri;
    if (!$preserveHost) {
        $new->updateHostFromUri();
    }
    return $new;
}

you clone and then directly change the properties (normally, you can't change private,), but you can only use withXXX these methods, you can be guaranteed to be immutable.

Menu