Create a Twig extension with filters

Code
<?php

// creating a twig filter Extension
class MyTwigFilters extends Twig\Extension\AbstractExtension
{
    public function 
getName()
    {
        return 
'MyFilters';
    }
    
    public function 
getFilters()
    {
        return [
            new 
Twig\TwigFilter('to_int', [$this'toInt']),
        ];
    }
    
    public function 
toInt($value)
    {
        return (int) 
$value;
    }
}

$templates = [
    
'filter-template' => 'Filtered value: {{ null|to_int }}',
];

echo 
'- template content:' PHP_EOL;
echo 
$templates['filter-template'] . PHP_EOL;

$loader = new Twig\Loader\ArrayLoader($templates);
$twig = new Twig\Environment($loader);
$twig->addExtension(new MyTwigFilters());

echo 
'- rendered template:' PHP_EOL;
echo 
$twig->render('filter-template');
Result
- template content:
Filtered value: {{ null|to_int }}
- rendered template:
Filtered value: 0
Used Versions
PHP 8.2, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8