configuration container vs globally define()ed contstants

Original thread by Pattle

I’m working on a project where all the global constants have been defined in a class called Constants, for example:

Then throughout the code something like Constants::getMaxValue() is used to get the value of a constant. This seems a very strange approach why wouldn’t you just use the define function in the outermost scope? I know define() can be quite slow but surely having to call a class property is not the most efficient way either?

EDIT: Also some of the functions have conditions in them hence why functions are called


My answer

 

Clearify the question: What is the advantage of a configuration container over globally define()ed configuration contstants?

The advantages are all the advantages that OOP offers: Data Abstraction and Encapsulation, Inheritance, Polymorphism and better design pattern integration.

The readers of this thread seem a little confused and focus on your class rather than the main question. To also clearify this, let me give you an example:

index.php:

This was originally an answer to a Stackoverflow question: http://stackoverflow.com/questions/21331766/constants-class-vs-global-constants/

Leave a Reply

Your email address will not be published. Required fields are marked *