Unhelpful `Unable to load the "MyCustomFunctions" runtime in "main" at line 1` error message
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:
// just as an easy to test example
public function print($var = '') {
return print_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 ❤️