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

Problem with custom function

Hi everyone I had written function to format seconds in this way: 67s -> 00:01:07 . Then I've wanted add this feature to volt:

$volt->getCompiler()->addFunction('toHHMMSS', function($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', ($t/3600), ($t/60%60), $t%60);
});

But there are some problems with that. Phalcon throws me this:

Parse error: syntax error, unexpected ':', expecting ',' or ';' in index.volt.php on line 94

What's wrong with this code?

Can you show your volt code where you use your function?

edited Sep '17
 $compiler = $volt->getCompiler();
 //Passing native PHP functions into Volt scope
            $compiler->addFunction('strtotime', 'strtotime');
            $compiler->addFunction('abs', 'abs');
            $compiler->addFilter('round', 'round');

            //Passing your own function/method etc.
            $compiler->addFunction('desiredName', function ($resolvedArgs) use ($compiler) {
                return '$this->myObjectInstance->someMethod(' . $resolvedArgs . ')';
            });

So they key here is escaping PHP, i.e. single qutation marks.

Hi @Mr-Eluzive when you add a function basically volt replace this function name with other code like yourRealFuntionCalled() the you have to create your custom funtion

@Jonathan show you a good example or check extending volt functionalities

Good luck

Sorry for lack of answer for a while but solution occurs very simple. All what I needed to do was placement the function in double quotes ;) Thank you for all of your answers.