Hi all
I have been coding in PhP for quite a few years now, but never used a constant ever. However I notice many people use it, but why? I don't see why, even in really big applications, one would define a constant rather than loading a variable or array with "constant" information and stick it in an include file.
I can only see sense in using constants if maybe REGISTER_GLOBALS is on, and people may change certain variables that are really important with $_GET or $_POST, but other than that, what am I missing?
Feedback greatly appreciated.
Regards,
H
Why use constants?
Moderator: General Moderators
-
Black Unicorn
- Forum Commoner
- Posts: 48
- Joined: Mon Jun 16, 2003 9:19 am
- Location: United Kingdom
It's just as you have said: constants can't be changed so they're better for anything that's, er, constant. If you see a constant in the code you know exactly what it's value is, and what it always will be. With variables, and with everything in the global scope, the value might be changed anywhere - perhaps by mistake by another programmer. That requires a lot more effort to figure out what's going on when you find a bug.
Of course, there shouldn't be anything in the global scope in the first place.
Of course, there shouldn't be anything in the global scope in the first place.
-
Black Unicorn
- Forum Commoner
- Posts: 48
- Joined: Mon Jun 16, 2003 9:19 am
- Location: United Kingdom
Thank you for your feedback. Sofar I've been safe without using constants, but I guess that's because I'm a lone programmer.
I think I'll start using them as of now as I'm reviewing my coding style, and am aiming for lazier practices as I'm getting on. I've just dipped into OOP, too, so it'll be yawns or headaches.
Best Wishes,
H
I think I'll start using them as of now as I'm reviewing my coding style, and am aiming for lazier practices as I'm getting on. I've just dipped into OOP, too, so it'll be yawns or headaches.
Best Wishes,
H
Constants make much sense when you build larger applications which need config-files. Example: certain directories are absolutely necessary to run an application, but what the name and location of the directory is, is at entirely configurable. This is configured once and saved in a file with a fixed location, the config-file. When the application starts, it reads out the config-file (in the case of PHP it's php.ini) and knows exactly where the resources it requires are located.
Re: Why use constants?
One particular problem is solved by them very nicely - magic numbers.Black Unicorn wrote:I don't see why, even in really big applications, one would define a constant rather than loading a variable or array with "constant" information and stick it in an include file.
Lets say you needed to divide some variable by 1440. Now, without knowing that we are talking about days, and the number of minutes in a day, you probably wouldn't have known what 1440 was. If we set a constant (MINUTES_IN_DAY), it becomes very obvious.
Now, on the flipside, if we instead make it a variable, and someone gets sloppy, suddenly 1440 is 720, because of a coding mistake. Thats the beauty of constants - they can't change due to a mistake. (In fact, if you try to change the value of a constant, and have E_ALL on, it will warn you).
Not to mention constants are nearly-universally named in all uppercase, which stands out very well, further preventing coding problems by making you aware of its unique nature.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Another reason I use them is for customization. Say you need a script to run for a bunch of different clients with different basic setups. Each one just gets their own include full of constants. At the company I work for, we do this all the time. (This also helps with magic numbers when you have 10 people working on a project)
-
ianlandsman
- Forum Newbie
- Posts: 24
- Joined: Thu Dec 30, 2004 9:50 pm
- Location: New York
I believe I've also read (though never tested) that constants are faster. So if you have a constant value that is accessed many times during a scripts execution it will be slightly faster than a normal variable.
Constants are also nice because they have no scope limitations. So you can use them in functions and so on without declaring them global.
As with all things you should use them appropriately because there semantic meaning is important. Making all your variables constants with define() is just as bad if not worse than not using them because your code will be harder to understand. Using them for actual constant values on the other hand makes your code easier to read and understand.
Constants are also nice because they have no scope limitations. So you can use them in functions and so on without declaring them global.
As with all things you should use them appropriately because there semantic meaning is important. Making all your variables constants with define() is just as bad if not worse than not using them because your code will be harder to understand. Using them for actual constant values on the other hand makes your code easier to read and understand.