Nullable getBody() for MessageInterface

57 views
Skip to first unread message

Dracony

unread,
Apr 20, 2015, 8:21:43 PM4/20/15
to php...@googlegroups.com
I can think of a lot of usecases with empty body, e.g. redirecting a user, processing a HEAD request, etc. For these cases it makes sense that the getBody method in MessageInterface is nulllable. Otherwise it forces some empty implementation to be done.

Anton Titov

unread,
Apr 20, 2015, 9:53:17 PM4/20/15
to php...@googlegroups.com
Hi,

why not just use stream with empty body?

Roman Tsjupa

unread,
Apr 20, 2015, 9:55:59 PM4/20/15
to php...@googlegroups.com

Well yes, that's the whole point. You cant just pass an empty string and be done with it, it has to be a StreamableInterface intaance. It doesnt seem elegant to create an empty one just for that purpose

--
You received this message because you are subscribed to a topic in the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/php-fig/c7amaAhnZbg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/981974d6-19e4-4425-9728-701551229d76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Dowling

unread,
Apr 20, 2015, 9:58:43 PM4/20/15
to php...@googlegroups.com
You can create the stream lazily on first access, so there is practically no overhead.
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.

Evert Pot

unread,
Apr 20, 2015, 10:01:42 PM4/20/15
to php...@googlegroups.com
On 2015-04-20 9:55 PM, Roman Tsjupa wrote:
> Well yes, that's the whole point. You cant just pass an empty string and
> be done with it, it has to be a StreamableInterface intaance. It doesnt
> seem elegant to create an empty one just for that purpose

You'll find that the opposite is true when you actually use the
interface. By making it optional, you basically force anyone to first
check if they received null, and only then do operations on the body.

This adds a lot of additional if statements for people using these, and
that's just so save the hassle of removing 1 object.

Evert

Roman Tsjupa

unread,
Apr 20, 2015, 10:01:55 PM4/20/15
to php...@googlegroups.com

Its not really about overhead or some optimization. I just like the idea of being able to use withBody(null).

Also allowing nullable geyBody turns it to an optional parameter. So insteaf of passing 'new StringStream('')' I could just use null.

Roman Tsjupa

unread,
Apr 20, 2015, 10:04:09 PM4/20/15
to php...@googlegroups.com

Well yes the null check is a downside, although in a lot of cases when reading a message a (string) cast will be used anyway:

echo (string) $response->getBody();

In this case null works perfectly

--
You received this message because you are subscribed to a topic in the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/php-fig/c7amaAhnZbg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
Message has been deleted

Roman Tsjupa

unread,
Apr 20, 2015, 10:13:00 PM4/20/15
to php...@googlegroups.com

That would fix nothing since StringStream('') would still be there just in a different place

On Apr 21, 2015 4:10 AM, "Anton Titov" <wolf...@gmail.com> wrote:
You can write your specific implementation which converts null argument to empty stream inside, this will keep getBody() behavior consistent.

Korvin Szanto

unread,
Apr 20, 2015, 10:19:59 PM4/20/15
to php...@googlegroups.com

I think it's clear that there is no "fix" needed here, I would argue that the null conditional is plenty to decide against this.


Roman Tsjupa

unread,
Apr 20, 2015, 10:22:14 PM4/20/15
to php...@googlegroups.com

Ah, well, lets leave it at that then. Its not that huge of a deal anyeway and the conditional argument does make sense

Bernhard Schussek

unread,
Apr 21, 2015, 3:41:36 AM4/21/15
to php...@googlegroups.com
I don't think that's a good change. Returning a NullStream instance seems much better to me.

--

Reply all
Reply to author
Forward
0 new messages