Create a Twig extension with filters

Code
<?php

// creating a twig filter Extension
class MyTwigFilters extends Twig_Extension
{
    public function 
getName()
    {
        return 
'MyFilters';
    }
    
    public function 
getFilters()
    {
        return [
            new 
Twig_SimpleFilter('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_Array($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 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6