Short Function Syntax

php.internals

Oladoyinbo Vincent

2 years ago
Hello Internals, I don't know maybe this discussion has been raised before, but i feel we should re-discuss this. `function` syntax is kinda verbose for naming methods in class, let's take a look at the below example: ```php class Greet { public function __construct( private string $name, private string $city ) {} private function init(): string { return "Hello {$this->name}, From {$this->city}"; } } ``` I know that the `fn` Syntax is already used for arrow functions, but what i am suggesting is that, can't we make `fn` to be used in naming methods in class? (this will be optional though), I mean like: ```php class Greet { public fn __construct( public string $name, public string $city ) {} private fn init(): string { return "Hello {$this->name}, From {$this->city}"; } } ``` The above code looks cool, readable and less verbose, what is your opinion on this?

Vinicius Dias

2 years ago
I honestly don't see any benefits. `function` isn't verbose, it's explicit. I prefer `function` over `fn` for methods. But despite what one may prefer, I believe changing the language syntax just because of "taste" probably isn't worth the effort.

Oleksii Bulba

2 years ago
> I know that the `fn` Syntax is already used for arrow functions, but what
i
> am suggesting is that, can't we make `fn` to be used in naming methods in > class? (this will be optional though),
Hello Oladoyinbo, Personally for me, if I see `fn` in methods instead of `function` I would expect it behave as `fn`-anonymous arrow function. E.g.: ```php class Example { public fn getMyProperty() -> $this->myProperty; } ``` What do you think about it? Best regards, Oleksii Bulba On Thu, 2 Nov 2023, 21:31 Oladoyinbo Vincent, <oladoyinbov@gmail.com> wrote:

Marcos Marcolin

2 years ago
Hi, I don't think this inclusion is a problem, but I also don't see any real benefits. Some parts of the syntax should be verbose to make it easier to read and understand, especially for those who are just arriving. Soon, people will want to be writing `pub` instead of `public`, or `priv` instead of `private` in method or property declarations. Just an addition to your example and my opinion. I prefer to define all properties in the class, instead of using property promotion in the constructor, maybe I have a serious problem. --- *Marcos Marcolin* Software Engineer | PHP www.marcosmarcolin.com.br