The "pipe operator" |>
allows you to chain multiple function calls in a more convenient way.
$result = "Hello World" |> 'htmlentities' |> 'str_split' |> fn($x) => array_map('strtoupper', $x) |> fn($x) => array_filter($x, fn($v) => $v != 'O');
This RFC was already declined, but we're sharing it here as another test RFC, and because it'd be interesting to learn people's opinion about it.
Could make php more flexible language.
replace string function names with first class callables and my in!
Make the code more readable like elixir does.
Used pipe operators in Elixir lang and loved it. This will help make code more readable.
I like the proposition, but I would use some special "variable" ($$) for piped value (see v1 proposition: https://wiki.php.net/rfc/pipe-operator) so you don't have to use closures inside pipe to place value
$name = 'my_user_name'
|> str_replace('_', ' ', $$)
|> strtolower($$)
|> ucwords($$)
|> trim($$);
I don't like mapping function names as strings too
That's awful, we see strings not function calls.
Voting Yes - but only if the RFC were updated to use First-class callable syntax only instead of strings for the functions. Would vote no looking at it based on the RFC alone, but looking at Roman's example I really wouldn't mind that. But I would 100% vote No if it were the original examples from the RFC text.
That in mind, would this RFC vote site benefit from a method to capture these types of votes? I.e. one where the vote would be No as proposed in the RFC currently, but could be a Yes with minor changes to update for other already accepted RFC.
really useful for chained method calls that already are crowded because of PHPs -> operator
I'm a fan of the method chaining pattern. This would be a nice way to have similar behaviour without having to create a class.
RFC shouldn't be a place to propose new syntax because laravel is messy
I think this would clean up a lot of messy code, much better than nesting functions
I like what the pipe operator provides, but I don't like the string-based syntax that's proposed. It feels weird to me that we're calling functions, but providing them as strings. Yes, we do this with call_user_func
but that's called as a parameter. The proposed syntax is very different to that.
This is a pattern already used in some tools and frameworks like Laravel with pipelines and since we have arrow functions and first-class callable I think this would bring a lot of possibilities to the language.
Also this is already proved really handy on other languages.
I don't love the syntax, but it would be nice to have a first party solution and is more readable than nesting, especially with callable syntax.
I also see it as a great addition for first-class callables in PHP and a logical next step for them.
This RFC proposes a way to have multi-line short closures — closures that don't need explicit use
statements, but can still have multiple lines and a return statement.
A new syntax for declaring the “set” operation visibility of an object property