Not sure if it's already been mentioned, but I've noticed this operator
would be useful when you want to modify something but need to transform it
before and after.

For example, I have a class that serializes/deserializes arrays of strings,
and I need to modify the array that it has encoded. The code currently
looks like this:

$id = StringList::encode(array_replace(StringList::decode($id), $replace));


The parallel between the encode and decode can be made more obvious by
writing it as a sequence of steps:

$id = $id
    |> StringList::decode($$)
    |> array_replace($$, $replace)
    |> StringList::encode($$);


Neat. A similar example is when you need to modify a percentage as a factor:

$percent = $this->modifyFactor($percent / 100, $this->moreParams(),
Blah::moreParams()) * 100;


This could, arguably, be better written better as:

$percent = $percent
    |> $$ / 100
    |> $this->modifyFactor($$, $this->moreParams(), Blah::moreParams())
    |> $$ * 100;




On Sat, Apr 30, 2016 at 5:58 AM, Sara Golemon <poll...@php.net> wrote:

> This is one of my favorites out of HackLang.  It's pure syntactic
> sugar, but it goes a long way towards improving readability.
> https://wiki.php.net/rfc/pipe-operator
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to