PHP RFC: Function Alias for class methods declaration
- Version: 1
- Date: 2025-07-24
- Author: Dmitrii Derepko, xepozzd@gmail.com
- Status: Draft (Later in the RFC process “Under Discussion”, “Voting”, and finally “Accepted”, “Declined” or “Implemented”)
- Implementation: https://github.com/...
Introduction
Modern languages use short constructions to boost developer experience. PHP as a modern language should follow in steps as well and re-use best practices of the others. I would like to make the fn
keyword available to declare class methods.
Modern languages, such as Kotlin, Rust, Python use short function declaration keywords such as fn
, fun
, def
.
PHP uses function
which is 4x longer that fn
, which is already in use in other parts of the language, such as closures.
Proposal
Make fn
keyword fully aliased for class methods:
<?php class A { fn main() { // "fn" instead "function" echo "main function"; } fn __construct() { // constructor's function can be changed as well echo "__construct function"; } function __destruct() { // classic "function" is still available echo "__destruct function"; } } ?>
This is fully equivalent to
<?php class A { function __construct() { echo "__construct function"; } function main() { echo "main function"; } function __destruct() { echo "__destruct function"; } } ?>
Examples
Remember that the RFC contents should be easily reusable in the PHP Documentation. This means, if at all possible, they should be runnable as standalone, self-contained code with the proof-of-concept implementation.
Simple example:
<?php class A { fn main() { // all methods are public by default echo "main function"; } public fn main() { echo "main function"; } protected fn main() { echo "main function"; } private fn main() { echo "main function"; } abstract fn main(); } ?>
Example showing an edge case:
<?php echo "Edge case"; ?>
Backward Incompatible Changes
- Breaks backward compatibility because of new syntax construction.
Proposed PHP Version(s)
PHP 9.0
RFC Impact
To the Ecosystem
What effect will the RFC have on IDEs, Language Servers (LSPs), Static Analyzers, Auto-Formatters, Linters and commonly used userland PHP libraries?
To Existing Extensions
Will existing extensions be affected?
To SAPIs
Describe the impact to CLI, Development web server, embedded PHP etc.
Open Issues
Make sure there are no open issues when the vote starts!
Future Scope
- [RFC] Function Alias for function declaration
Modern languages provide developers simple ways to define most popular constructions, such as short constructors, short functions, omitting mindless keywords and etc.
This RFC make PHP a step closer to a modern PHP look:
<?php class MyService(private AnotherService $service): AbstractService(prefix: 'my') { fn getApiVersion() => 1; fn doProxy() { return $this->service->main(); } } ?>
Instead, classic syntax is still available and much verbose:
<?php class MyService extends AbstractService { function __construct(private AnotherService $service) { parent::__construct(prefix: 'my'); } function getApiVersion() { return 1; } function doProxy() { return $this->service->main(); } } ?>
This examples uses short constructor RFC syntax, single expression function syntax and have function -> fn alias
Question / Answer
Should be "function" deprecated?
Yes, it should, but with no deprecation warning. Modern codebase will use short syntax (on demand), old projects/libraries won't be re-writed, so classic “functions” should live longer.
Will single function declaration be affected as well?
Yes, in another RFC.
Can I use both <php>fn</php> and <php>function</php> keywords in the same class?
Yes, as you wish:
<?php class A { fn get() {...} function set($value) {...} }
As it's fully-alised it can be replaced vise-versa. The reason to use it so is up to you.
Voting Choices
Pick a title that reflects the concrete choice people will vote on.
Please consult the php/policies repository for the current voting guidelines.
Patches and Tests
Links to proof of concept PR.
If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.
Implementation
After the RFC is implemented, this section should contain:
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
References
Links to external references, discussions, or RFCs.
Rejected Features
Keep this updated with features that were discussed on the mail lists.
Changelog
If there are major changes to the initial proposal, please include a short summary with a date or a link to the mailing list announcement here, as not everyone has access to the wikis' version history.