Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

31–40 of 282 posts

Re: PHP 8.5 adds pipe operator

#32
post #23

I tried to emulate something similar with PHP at one point. But the problem with PHP was parameter order. Especially in functions like array_key_exists() the array element is the 2nd parameter, while pipe operator expects the object to work on be the 1st parameter, the array in these cases. I believe they have solved this problem by now. Though no idea how.

The usual solution is to wrap it with a closure.

    function($x) { return array_key_exists('needle', $x); }
Or using the arrow function syntax:

    fn($x) => array_key_exists('needle', $x)
The same trick also helps when you need to use functions with mandatory extra parameters, functions with pass-by-value parameters, etc.

Re: PHP 8.5 adds pipe operator

#33
post #29

I like it. I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chainable. I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator

PHP string / array functions are consistent. string functions use (haystack, needle) and array functions use (needle, haystack) because that's the way the underlying C libraries also worked

They're not though.

array_filter takes (arr, callback)

https://www.php.net/manual/en/function.array-filter.php

array_map takes (callback, arr)

https://www.php.net/manual/en/function.array-map.php

Re: PHP 8.5 adds pipe operator

#34

I like it. I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chainable. I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator

Agree, the ... syntax feels confusing when each fn($x) in the example uses $x as the name of its argument. My initial instinct would be to write like this: `$result = $arr |> fn($arr) => array_column($arr, 'tags') // Gets an array of arrays |> fn($cols) => array_merge(...$cols)` Which makes me wonder how this handles scope. I'd imagine the interior of some chained function can't reference the input $arr, right? Does…

You can write it this way. The parameter name is arbitrary. And no, to my knowledge you can't access the var from the previous scope

Re: PHP 8.5 adds pipe operator

#35

Why doesn't PHP remove the horrid $ symbol for variables and the -> symbol for calling methods? I think those alone would do a lot more for its perception and adoption than adding the pipe operator.

I actually don’t mind them, and I’ve been out of daily PHP work for a few years now. When I see people denote internal variables with _ or elements with $ in JS, it rubs me the wrong way, but in PHP the $ is kind of nice. I also prefer the look of ->, it’s _cool_

Other languages have all sorts of oversized arrows, like ==> and >>>.

-> in PHP and C++ looks clean by comparison.

I'll never forgive them for the brain fart they made of the namespace separator, though.

Re: PHP 8.5 adds pipe operator

#36

The stdlib is so inconsistent this will be a nightmare. Optionally with a better language you know what order params as passed (array_map / array_filter), but in PHP its an coin toss. This feels very bolted on and not suited for the stdlib at all. PHP devs should instead FIRST focus on full unicode support (no, the mb_real_uppercase wont do), and only then focus on a new namespaced stdlib with better design.

This.

We definitely need a better stdlib with appropriate data structures

Re: PHP 8.5 adds pipe operator

#37
I love the pipe operator - one of the things I dig about Elixir though many languages have it. It's so much easier to reason about:

  $result = $arr
    |> fn($x) => array_column($x, 'tags')
    |> fn($x) => array_merge(...$x)
    |> array_unique(...)
    |> array_values(...)
VS array_values(array_unique(array_merge(...array_column($arr, 'tags'))));

Re: PHP 8.5 adds pipe operator

#38

Why not just make types psuedo-objects? $myString.trim().replace("w", "h"); Which has the advantage of also offering a clean alternative to the fragmented stdlib.

I agree. But in PHP it would probably be like this:

$myString->trim()->replace("w", "h");

Re: PHP 8.5 adds pipe operator

#39
post #38

Why not just make types psuedo-objects? $myString.trim().replace("w", "h"); Which has the advantage of also offering a clean alternative to the fragmented stdlib.

I agree. But in PHP it would probably be like this: $myString->trim()->replace("w", "h");

Because pipes don't care about the type your function returns. And you don't need hundreds of methods on each type just in case. You just pipe the result of the previous function to the next one.

And those functions can be business logic, or validation, or... Not just object methods

Post reply on HN