PHP 8.5 adds pipe operator
31–40 of 282 posts
Re: PHP 8.5 adds pipe operator
#32I 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.
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
#33I 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
array_filter takes (arr, callback)
https://www.php.net/manual/en/function.array-filter.php
array_map takes (callback, arr)
Re: PHP 8.5 adds pipe operator
#34I 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…
Re: PHP 8.5 adds pipe operator
#35Why 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_
-> 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
#36The 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.
We definitely need a better stdlib with appropriate data structures
Re: PHP 8.5 adds pipe operator
#37 $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
#38Why 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.
$myString->trim()->replace("w", "h");
Re: PHP 8.5 adds pipe operator
#39Why 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");
And those functions can be business logic, or validation, or... Not just object methods