Search code examples
phpconstants

PHP: How does one define a constant that is accessible by multiple files?


Basic PHP design questions: I want to define a couple of constants that are accessible to all the files in my website. If I use define() I have to do that for each class which seems silly in that if/when I change that constant for a future release, I'll have to change it in each file. One option is to put it in a file and then require_once that file in all my source files, but that seems excessive too. Is there a place I can define the constant so that it is global across files?

For example:

define('MAX_ELEMENTS', 5);

At some point in the future, I may change this to 6. I use MAX_ELEMENTS in my business logic everywhere.


Solution

  • It's very common to have a single, canonical include file used by all (or most) scripts on your website. PHP even has auto_prepend_file to allow for this.