The best way to include php functions


I have written a php snippet that contains some php functions().

As of today I just included this snippet at the beginning of my Base Template:

[code][[MySnippet]]<!doctype html>


Today I added $modx->sendErrorPage(); in my code and it turns out that my snippet is called one more time when loading the error_page.

The problem is my snippet contains php functions I get error messages when they are called more than once.

I know I could use if(!function_exists(myFunction)) to bypass the error messages but i think this is not a neat solution. I’d prefer my snippet to be executed only once at the beginning.

I tried to put my functions in a plugin on the OnHandleRequest event but this event is also called a second time when sending to error page.

Per chance would you have other solution for me ?

Stay safe :slight_smile:

To avoid errors that functions already exists, you could put them in a file and use include_once/require_once to load that file.

Regardless though, your snippet will always run twice if your error page uses the same template as the rest.

Thank you Markh ! :slight_smile:

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.