Since a PSR-7 Response is supposed to be immutable, why can I write this disturbingly "mutating" piece of code?
public function controller(Response $response): Response
{
$response->getBody()->write("Hey.");
return $response;
}
It seems to me that while the Response in itself is immutable, meaning that we get a new object when we call $response->withHeader(…)
for instance, we still can (and usually do) mutate its Body object (not the least important part of the response).
Isn’t that inconsistent? Or is it perfectly sensible? It just seems quite weird to me.
Your question is directly answered in the meta for PSR-7: