Immutability and PSR-7 compatibility
mainThe Response class uses ResponseImmutabilityTrait to provide PSR-7 compatible methods that return a new instance rather than modifying the existing one. This ensures the response remains immutable.
Supported methods:
withStatus($code, $reasonPhrase = ''): Returns a new instance with the specified status.withAddedHeader($name, $value): Returns a new instance with the header appended.withoutHeader($name): Returns a new instance without the specified header.withHeader($name, $value): Returns a new instance with the header replaced.withProtocolVersion($version): Returns a new instance with the new protocol version.withBody(StreamInterface $body): Returns a new instance with a new body stream.
// The original $response remains unchanged
$newResponse = $response->withStatus(201)->withHeader('X-Custom', 'Value');