XF 2.1 Deprecated method alert: XF\Template\Templater::fn is deprecated from XF 2.1.3 and will be removed eventually.

Chris D

XenForo developer
Staff member
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'];

Whilst the XenForo code base will not be able to benefit from this functionality for many years, it does mean that the fn part above becomes a reserved keyword which will be an issue if we want to support PHP 7.4 in the future. Unfortunately, we use fn as a method name within our Templater.

From XF 2.1.3 we have renamed the XF\Template\Templater::fn() method to XF\Template\Templater::func(). Because this would very likely break existing code we have kept the fn() method but it now simply calls the func() method.

We urge you from 2.1.3 onwards to replace any calls to fn() in your own code to func(). At some point in the near-ish future, we will be adding PHP 7.4 support to XF 2.1 and at that point, we will likely have to remove the fn() method.
 
Last edited:

jmurrayhead

Well-known member
What does XF\Template\Templater::fn() aka func() do?

I've never been a fan of abbreviations, but obviously, you can't call it function() (probably reserved) either...or can you? :D
 

Chris D

XenForo developer
Staff member
It calls templater functions. Things like {{ link() }} and {{ phrase() }}.

You're right, we can't call it function. It is crossing my mind whether we should anticipate them reserving func as a keyword at some point, but that would just be greedy, surely?
 

jmurrayhead

Well-known member
It calls templater functions. Things like {{ link() }} and {{ phrase() }}.

You're right, we can't call it function. It is crossing my mind whether we should anticipate them reserving func as a keyword at some point, but that would just be greedy, surely?
Maybe lean towards a naming convention like Utility or Helper or something. I dunno. Sometimes naming stuff is hard. :LOL:
 

Xon

Well-known member
Ugh, this impacts a bunch of my add-ons. I want to keep php 7.4 compatibility without needing to version bump stuff. Yuck

You're right, we can't call it function. It is crossing my mind whether we should anticipate them reserving func as a keyword at some point, but that would just be greedy, surely?
templateFunc could work :p
 

Mythotical

Well-known member
It calls templater functions. Things like {{ link() }} and {{ phrase() }}.

You're right, we can't call it function. It is crossing my mind whether we should anticipate them reserving func as a keyword at some point, but that would just be greedy, surely?
Another method you could possibly use just in case they reserve
Code:
func()
would be
Code:
tpl
or
Code:
tmpl
since that would define it properly what it is meant for.
 

Arty

Well-known member
Thanks for heads up, this does impact some of my code.

Its a shame they decided not to use syntax like JavaScript because it would complicate parsing a bit:
PHP:
$container['router'] = (Container $c) => $c['router.public'];
 
Last edited:

Chris D

XenForo developer
Staff member
Thanks for heads up, this does impact some of my code.

Its a shame they decided not to use syntax like JavaScript because it would complicate parsing a bit:
PHP:
$container['router'] = (Container $c) => $c['router.public'];
Yeah I said the same here:
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 🤷‍♂️
Dart is an interesting programming language. They've implemented both types of function without a keyword at all:
Code:
(arg) => Return();
And:
Code:
(arg)
{
    return Return();
}
 

Xon

Well-known member
I suspect it is the case that the php parser/ast isn't capable enough to handle contextual keywords without a lot of work. And for whatever reason they need to know if it is a closure before parsing the RHS.
 
Top