Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful Unable to load the "MyCustomFunctions" runtime in "main" at line 1 error message #4000

Closed
pgorod opened this issue Feb 23, 2024 · 0 comments

Comments

@pgorod
Copy link
Contributor

pgorod commented Feb 23, 2024

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 ❤️

@twigphp twigphp locked and limited conversation to collaborators May 1, 2024
@fabpot fabpot converted this issue into discussion #4065 May 1, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

1 participant