Answer the question
In order to leave comments, you need to log in
How to execute a function in a static variable?
I have some View class.
Its instance is created during controller operation.
For example:
$view = new View('index.twig');
$view->render();
class View
{
public function __construct($tpl = "")
{
$loader = new Twig_Loader_Filesystem(TPL_DIR);
$this->twig = new Twig_Environment($loader, self::$twigParams);
// Расширение функционала Twig
$twig->addFunction(new Twig_SimpleFunction('file_exists', function($filename)
{
return file_exists(ROOT.$filename);
}));
// Здесь ещё много таких функций и фильтров
if($tpl)
$this->setTpl($tpl);
}
...
}
// controllers/init.php
View::$extFunc = function(Twig_Environment &$twig)
{
$twig->addFunction(new Twig_SimpleFunction('file_exists', function($filename)
{
return file_exists(ROOT.$filename);
}));
}
// View.class.php
class View
{
public static $extFunc;
public function __construct($tpl = "")
{
...
if(is_callable(self::$extFunc)) // Вот эта проверка проходит, значит там-таки функция
self::$extFunc($this->twig); // Вот тут ошибка "Function name must be a string"
...
}
}
Answer the question
In order to leave comments, you need to log in
The issue was resolved by creating an extension of the Twig_Extension class in which you can override / add everything you need.
twig.sensiolabs.org/doc/2.x/advanced.html#creating...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question