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

Custom function to access globally

I want to define a custom function and use that in Volts and contollers simulteniously. I tried this way

$di->set('en2bn', function ($input) {
    $bn_digits = array('০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯');
    $output = str_replace(range(0, 9), $bn_digits, $input);
    return $output;
});

and tried to access within controller this way

$this->en2bn(8);

And got error. Can you please advice me?



7.7k
Accepted
answer
edited Apr '15

Just create a seperate class where you have all your custom functions, instead of using $di->set()