We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt and custom function

Here is my code

/**
 * Setting up the view component
 */
     $di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            $volt->getCompiler()->addFunction(
                'paymentStatus',
                function($key)
                {
                    return @"Info::paymentStatus({$key})";
                }
            );

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
});

And the error message (expected to be honest to say)

Strict Standards: Non-static method Info::paymentStatus() should not be called statically, assuming $this from incompatible context in /home/zxcvbnm/public_html/app/cache/_home_zxcvbnm_public_html_app_views_invoice_admininvoice.volt.php on line 49

How can I call method dynamically?

edited Sep '15

You have to return a valid PHP code:

$volt->getCompiler()->addFunction('paymentStatus', function($resolvedParams) {
    return "Info::paymentStatus(".$resolvedParams.")";
});

This still won't work if paymentStatus is an instance method. Then you need an instance for the Info class... you could set it up as a service, or create a static method to return an instance.



7.6k
Accepted
answer

I fixed the issue just using add keyword static. I mean public static function...