How can PHP extensions be utilized to facilitate the execution of PHP methods within Twig templates?

To facilitate the execution of PHP methods within Twig templates, PHP extensions can be utilized. These extensions can be used to create custom functions or filters that can be called directly within Twig templates to perform specific tasks or calculations.

// Register a custom function in PHP extension
$twig->addFunction(new \Twig\TwigFunction('custom_function', function ($arg1, $arg2) {
    // Perform some logic using $arg1 and $arg2
    return $result;
}));