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

Where to write a php function accesible to all my web app

Hello, I need to write a couple of functions accesible all over the web app. Where should I write this? And how do I access it then? Thanks.

You can create a Utils class that will have those functions in there and get it from there. For instance:

require 'library/Utils.php';

// Statically (personally I hate static calls)
$var1 = Utils::myFunc();
$var2 = Utils::yourFunc();

or you can register it in the DI container:

require 'library/Utils.php';

$di['utils'] = function () {
    return new Utils();
}

and then from a controller:

$var1 = $this->utils->myFunc();
$var2 = $this->utils->yourFunc();

The above will not use statically defined functions.



18.9k

Thanks Nicolaus :D