You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This was my function definition as a method of class MyCustomFunctions:
// just as an easy to test examplepublicfunctionprint($var = '') {
returnprint_r($var, true);;
}
SOLUTION The problem was that the way I was loading the function was not consistent with the function declaration. I had forgotten to make the function declaration public static function, with that static in there.
After adding the static it matches the :: callable used when loading the function.
Obvious, once you see it, but terribly not-obvious from the error message... it took me a long time to figure this one out. The error was only happening at render time, not when running addFunction, that didn't make things any easier to diagnose.
I suggest improving the error message, if possible. Thanks. I love Twig ❤️
This discussion was converted from issue #4000 on May 01, 2024 12:00.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Twig version: v3.5.1
PHP version: 7.4
I'm copying this from an answer I just left on stackOverflow:
I was getting:
Unable to load the "MyCustomFunctions" runtime in "main" at line 1
The way I am loading my functions is not inside an extension, but one by one, like so:
$twig->addFunction(new TwigFunction('owner', 'MyCustomFunctions::print'));
This was my function definition as a method of class
MyCustomFunctions
:SOLUTION The problem was that the way I was loading the function was not consistent with the function declaration. I had forgotten to make the function declaration
public static function
, with thatstatic
in there.After adding the static it matches the
::
callable used when loading the function.Obvious, once you see it, but terribly not-obvious from the error message... it took me a long time to figure this one out. The error was only happening at
render
time, not when runningaddFunction
, that didn't make things any easier to diagnose.I suggest improving the error message, if possible. Thanks. I love Twig ❤️
Beta Was this translation helpful? Give feedback.
All reactions