Creating and using Twig globals

Code
<?php

class MyGlobal
{
    public function getFoo()
    {
        return 'foo';
    }
    
    public function getBar()
    {
        return 'bar';
    }
}

// creating Twig Global as Extension
class MyTwigGlobals extends Twig\Extension\AbstractExtension
{
    public function getName()
    {
        return 'MyGlobals';
    }
    
    public function getGlobals()
    {
        return [ 'my_global' => new MyGlobal()];
    }
}

$path = __DIR__ . '/templates/';
$file = 'global.html.twig';

echo '<b>- template content:</b>' . PHP_EOL;
echo file_get_contents($path . $file) . PHP_EOL;

$loader = new Twig\Loader\FilesystemLoader($path);
$twig = new Twig\Environment($loader);
$twig->addExtension(new MyTwigGlobals());

echo '<b>- rendered template:</b>' . PHP_EOL;
echo $twig->render($file);
Result
- template content:
getFoo() contains: {{ my_global.foo }}
getBar() contains: {{ my_global.bar }}
- rendered template:
getFoo() contains:
getBar() contains:
Used Versions
PHP 8.3, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8