Code Monkey home page Code Monkey logo

Comments (2)

rbaarsma avatar rbaarsma commented on May 22, 2024

I'm baffled, but when I change my code to do this, it works

$request = $this->requestFactory->createRequest($method, $this->host.$url)
    ->withBody($this->streamFactory->createStream($body));

echo (string)$request->getBody(); // now returns the $body as expected!

I really did not expect php to see direct chaining differently than using a local variable in between.

In any case, I stlil think this is very werid behavior and my first code should work too or otherwise give some kind of comprehensible exception why I shouldn't do that..

I still don't understand why the two statements are different

from psr7.

Zegnat avatar Zegnat commented on May 22, 2024

Every time you call a method on an object, it actually returns a completely new object rather than updating the existing one. So when you call $request->withBody(…) this call returns a new instance of a Request object while the original object $request is unchanged.

By making sure you redefine the variable $request you can make sure you always have the latest created object ready to use. You can do this by either chaining the method calls (because the final output returned gets assigned to your variable) or by redeclaring the variable like so:

$request = $requestFactory->createRequest($method, $url);

$body = json_encode(['test' => 'test']);
$request = $request->withBody($streamFactory->createStream($body));

echo (string)$request->getBody(); // empty string "" (!)

Note how I have updated line 4 of your example code to start with $request = .

This is because PSR-7 objects are defined to be immutable. Meaning (in short) they cannot be changed after being created. To quote from the PSR-7 definition of a Request object:

 * Requests are considered immutable; all methods that might change state MUST
 * be implemented such that they retain the internal state of the current
 * message and return an instance that contains the changed state.

You can read more about this on the PHP-FIG’s website for PSR-7, under “Why value objects?”.

I am closing this issue as there is nothing here for this library to address. But if you have any follow-up questions, feel free to post them as I am happy to answer them :)

from psr7.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.