I heard so many times that use define() to create global constant is a bad solution. But I've never heard why it is so.
And what is the way it should be done instead?
I heard so many times that use define() to create global constant is a bad solution. But I've never heard why it is so.
And what is the way it should be done instead?
Probably you've read something about not polluting the global namespace. At least that's the biggest reason why should you try to avoid global constants - and variables and functions for that matter. However there are valid use cases where a global definition makes sense.
In most cases though the constant belongs to something. That something being a class it's worth defining the constant within the class.
If you define a constant that has a long prefix (eg. VALIDATOR_EMAIL_PATTERN) that is a sign of a possible class related constant definition (Validator\Email::PATTERN)
Global definitions have more chance to collide thus making parts of your code harder to re use, since two different library could try to define constants with the same name for their our purpose.