Fixed php 7.4 compatibility - fn is now a keyword

Xon

Well-known member
Affected version
2.1.2

Unfortunately the fn keyword must be a full keyword and not just a reserved function name.

Ilija Tovilo analyzed the top 1,000 PHP repositories on GitHub to find usages of fn. The gist provides more information, but the rough findings are that all known existing usages of fn are in tests except one case where it is a namespace segment. (The namespace use happens to be in my own library, and I'm happy to rename it.)

This affects; XF/Template/Templater::fn
 
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.3).

Change log:
Rename Templater::fn function to Templater::func. Templater::fn remains but will be removed in the near future.
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 
This is somewhat of a pain but so be it. We've renamed the fn function to func but the fn function still exists, pointing to func and marked as @deprecated. We'll have to remove it, probably before XF 2.2 which is not ideal - I'm sure people will start running into it pretty quickly so whichever XF 2.1 release adds PHP 7.4 compatibility will ultimately remove this now deprecated method.

It's strange. Many languages have implemented fat arrow functionality without the need for a special keyword. I find it hard to believe they wouldn't have been able to come up with a certain construct. Many languages omit the fn bit entirely without any ambiguity 🤷‍♂️

In case anyone is wondering what we're talking about, PHP 7.4 will introduce a new shorthand for defining functions inline. Currently we do things like this:
PHP:
$container['router'] = function (Container $c)
{
   return $c['router.public'];
};

From PHP 7.4 onwards it can be shortened to:
PHP:
$container['router'] = fn(Container $c) => $c['router.public'];
 
Top Bottom