Global Constants

How badly does global constants weight a php application down.

Lets say I have 30 global constants a constants.php page, will this cause performance issues in my application?

Problems of hidden dependency aside, constants are still preferable to “magic numbers”

Do a microtime() and memory_get_usage() before and after including the constants file and you’ll have your answer… which is going to be that the difference is negligible.

However, constants and globals should be used with the consideration that you’re introducing non-obvious dependencies throughout your application wherever they’re used.

Having 30 global constants – or even 200 – isn’t going to noticeably impact performance on your application at all.

The question you should be asking instead is if constants are what should be used at all for configuration. I did a project several years ago where I used constants for all configuration like paths, database, etc. It started to become a problem when we did some filesystem changes as a result of scaling out. Essentially, we had the equivalent of an “upload path” constant that was used along with the username appended to upload files to. As we scaled out, we ended up moving all images and static files and user uploads to an off-site CDN with remote servers. Because we used a constant for the upload path and url, we had to make a new whole new configuration system and then go back in and change every single place an old config constant was used, because file locations affect not only the paths but the resulting URLs as well. It was a huge mess, and I have never used constants for configuration ever again. Constants are both global and immutable (unchangeable), which can be a bad thing for configuration, especially if parts of your configuration depend on logins, users, accounts, subdomains/domains, or other dynamic variables.