There must be a better way to get a similar syntax while mitigating some of those issues.

My biggest concern was the '$$' value, being the single cause of the most problems previously listed.

Since PHP is infamous for its needle/haystack problem with its core functions, I figure it’s best to make this feature more consistent and always force the real value to be piped through to the place the developer wants each time, even if it’s a little verbose.

How can we get the real value though? It’s not easily possible while chaining because we can’t define and then access a variable in our main scope until the chain is complete, right?

Right, but with one exception — making use of the $GLOBALS superglobal offered by PHP to share the value between scopes as it’s being changed. We can get access to this with a property inside the class that’s returned, making it easy to use without worrying about the internals.

Once we can access the raw value, it’s easy for us to bring all of the function calls back up to vanilla PHP instead of abstracted to strings, and we can end up piping functions like so:

$taken = take('https://blog.sebastiaanluca.com/');



$taken->pipe(parse_url($taken->value, PHP_URL_HOST))

->pipe(explode('.', $taken->value))

->pipe(reset($taken->value))

->value;

This solves most of the issues brought up above — you now get the benefits your IDE affords you, you can use objects and methods you’ve already got instantiated, you don’t need to use closures for anything, et cetera.

Unfortunately there’s still a couple of problems with it:

We need to access the function’s return value before the piping is done, so there must be an intermediary variable set. This may lead to extra code when only a couple of functions are piped, but not so much of a problem where you need to pipe lots of things together (the RFC gave a real world example where FBShipIt pipes 11 functions together for 1 output).

It’s still pretty verbose since we need to type ->pipe() after each function piped.

Regardless, this source for this solution is available in the following Gist: