This RFC proposes an alternative syntax for single-expression functions using the =>
operator instead of curly braces and explicit return
statements.
This syntax provides a concise way to declare simple functions without the overhead of traditional function body syntax.
This RFC introduces a shorthand syntax for functions that consist of a single return statement.
The proposed syntax uses the =>
operator to denote that the function directly returns the result of the expression.
Modern codebases typically contain numerous entities, models, forms, data transfer objects (DTOs), and value objects (VOs) with many getter and setter methods. These methods are usually simple one-liners that create significant cognitive overhead with minimal functional value.
Concise syntax makes it easier to associate function names with their return values, reducing mental parsing overhead.
PSR-compliant syntax requires multi-line formatting:
function getName() { return "Name"; }
Even compact syntax contains redundant elements:
function getName() { return "Name"; }
When reading traditional function syntax, developers mentally process:
function getName() { return "Name"; }
The brain performs this parsing:
function
- skip (boilerplate)getName
- identify function purpose()
- note no parameters{
- skip (boilerplate)return
- skip (expected keyword)"Name"
- identify return value;
- skip (boilerplate)}
- skip (boilerplate)Effectively, the brain extracts:
getName() "Name"
This syntax directly represents the mental model: function maps to value, making the code more readable and reducing the cognitive load required to understand simple getter/setter methods.
function functionName(parameters): returnType => expression;
This is functionally equivalent to:
function functionName(parameters): returnType { return expression; }
Basic function:
// Short syntax function getVersion(): int => 1; // Equivalent traditional syntax function getVersion(): int { return 1; }
Function with parameters:
// Short syntax function add(int $a, int $b): int => $a + $b; // Equivalent traditional syntax function add(int $a, int $b): int { return $a + $b; }
Class methods:
class Calculator { // Short syntax public function multiply(int $a, int $b): int => $a * $b; // Equivalent traditional syntax public function divide(int $a, int $b): float { return $a / $b; } }
This change introduces no breaking changes. The proposed syntax would currently result in a parse error, making it safe to implement.
PHP 8.5
The implementation is purely lexical—during parsing, the short syntax is transformed into the equivalent traditional syntax before further processing. This ensures no impact to SAPIs.
Nope.
Implementation does compile-time changes that do not affect Opcache.
Nope.
Nope.
Make sure there are no open issues when the vote starts!
Parent scope won't be captured as arrow functions do.
Current RFC do lexer/parser modification allows to shorten typical code.
This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.
Include these so readers know where you are heading and can discuss the proposed voting options.
Links to any external patches and tests go here.
If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.
Make it clear if the patch is intended to be the final patch, or is just a prototype.
For changes affecting the core language, you should also provide a patch for the language specification.
Keep this updated with features that were discussed on the mail lists.