Do you folks use constants as a matter of course with PHP and should I start too?
I never have. Unless it's a constant built into PHP (or built into a CMS/script that I'm working with), I don't use them.
I never liked constants much - for the same reason I don't like globals. Values should be declared in the appropriate scope, and passed to where they need to be. I'd much rather declare a variable in a config file and pass it to my functions than make use of a constant.
- Walkere
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #2 on: Feb 02, 2008, 09:14:04 am »
Constants are cool for settings like your database parameters, use them only if you don't need to change the values later in your script
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #3 on: Feb 02, 2008, 10:28:31 am »
I use constants too for settings in my applications (db settings, session settings, etc.)
It is cool to use them, because this way you are sure that you wont override an important variable.
Bill Cosby is my Father
Posts: 4
28 credits Members referred : 0
« Reply #4 on: Feb 02, 2008, 05:26:25 pm »
I always knew they existed, what they did, what they are for and how to use them.
The thing is I have habitual variables that I use in pretty much everything I do. For example i use $handle for files, $dbconnect for db connection values, $sql for holding SQL query strings etc etc
So I guess I have always used these habitual variables to store my constant values, thus I know automatically not to use them. They are my own version of reserved variables I suppose.
I do see how this is not good practise when developing products that will go to a wider community, or applications that will be potentially modified by a 3rd party in the future.
Atari ST fan
Gender:
Posts: 8
48 credits Members referred : 0
« Reply #5 on: Feb 02, 2008, 10:14:29 pm »
Just remembered one situation in which constants are VERY helpful.
Let's say you want to create a set of flags as options for a function. You decide to use the bits in a number to toggle the flags on and off.
So "Flag 1" is represented by the first bit, "Flag 2" is represented by the second bit, etc. You can define these as constants (1, 2, 4, 8, 16, etc) and then easily combine them with the bitwise "or" function...
For example...
Code:
define("FLAG1", 1); define("FLAG2", 2);
$flags = FLAG1 | FLAG2;
if ($flags & FLAG2) { // Flag 2 is set, so do whatever we need to do }
Without constants, it becomes one heck of a headache to work with bitwise operators like this.
- Walkere
Community Supporter?
Hunky Junky Monky Man!
Gender:
Posts: 68
436 credits Members referred : 0
Schwa?
« Reply #6 on: Feb 09, 2008, 04:00:40 am »
At work, we've used them for database and other credentials.
I personally don't use them on my projects.
Plus - for fun - run benchmarks against them - not that great.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7563