2015-01-30 18:07 GMT+03:00 Andrea Faulds <a...@ajf.me>: > This approach wouldn’t solve the problem you’re describing. You *still* > need to produce a new request object, because the request object is > immutable. The mutability of its *properties* isn’t the issue.
Thank you for this answer. However, pay an attention to the last part of my suggestion. To create a mutable copy of immutable object it will be possible just to make an assignment (this will create a copy/clone) to a local mutable variable and adjust it for your needs. example: public function handleRequest(const Request $request) { // Mark $request as immutable for body // we can not change $request variable itself // but if need, we can only initialize a copy of it // $request->url = 'https://evil.com'; // Exception will be there $mutableSubRequest = $request; // copy of immutable object or maybe a direct Copy-on-Write? $mutableSubRequest->uri = 'https://example.com'; // It's ok to modify our local copy of an object $this->kernel->handleRequest($mutableSubRequest); } 2015-01-30 18:07 GMT+03:00 Andrea Faulds <a...@ajf.me>: > Hi Alexander, > > > On 30 Jan 2015, at 13:07, Alexander Lisachenko <lisachenko...@gmail.com> > wrote: > > > > Hello, internals! > > > > Today I was looking at PSR-7 and discovered this part of code: > > > > $body = new StringStream(json_encode(['tasks' => [ > > 'Code', > > 'Coffee', > > ]]));; > > $request = $baseRequest > > ->withUri($uri->withPath('/tasks/user/' . $userId)) > > ->withMethod('POST') > > ->withHeader('Content-Type' => 'application/json') > > ->withBody($body); > > $response = $client->send($request); > > > > What is wrong here? Emulated immutability. All methods will create a > > separate instance of request, so > > $baseRequest->withUri()->withMethod()->withHeader()->withBody() will > create > > total 5 object instances. It's a memory overhead and time consumption for > > each method call. > > Yes, I also think this is unfortunate. > > > What I want to discuss is true immutability flag for variables and > > parameters. There are a lot of languages that use "final" or "const" > > keywords to prevent modification of variables. We can use this approach > by > > extending language syntax as following: > > This approach wouldn’t solve the problem you’re describing. You *still* > need to produce a new request object, because the request object is > immutable. The mutability of its *properties* isn’t the issue. > > If you want to avoid creating five different objects, you’d need to > implement value-type objects that are passed by value and use > copy-on-write. Basically, you’d need to re-add PHP 4 style classes. > > Thanks. > -- > Andrea Faulds > http://ajf.me/ > > > > >