I’ve recently started using classes in my projects, and one of the uses is to set and store global constants in a private array. I’m wondering if this is common practice, bad and, most importantly, slow if I’m doing lots of calls to the fetch function in the class (lots as in, whenever I need the site name, an email address or MySQL details).
I’m not posting the code because it’s really basic; just a function that returns the value of an item in the array with the key given.
Don’t worry about that.
Going this way, you will come to patterns Singleton and Registry. Don’t use these patterns, it’s anti-patterns.
Try to build design by the SOLID principles, and your objects will be coupled less and less. The Site Name will be stored in the class, which will output content to the page, and other classes will not need this constant. The Email Address will be stored only in the Mailer class (or in his configuration file), and other classes will not need this constant. And so on.
Class, which contain all constants and all configurations it’s kind of a ‘God object‘, and it’s an anti-pattern (bad practice) too.
Don’t worry about performance of getters and setters.