Page 1 of 1
define( ) or not
Posted: Fri Feb 01, 2008 7:59 am
by kryles
Hi,
I'm just wondering if using define() a lot takes up many resources? For instance I'm making fields for a registration page as such:
Code: Select all
reg.html
<tr>
<td>
<?php echo LASTNAMELABEL; ?>
</td>
<td>
<input type ............>
</td>
</tr>
constant.php
<?php
define("LASTNAMELABEL", "*Last Name: ");
?>
I'm not asking about the code syntax, I would just like to know if define is a lot more greedy than simply doing
Or if I'm going overkill and I should just type *Last Name: directly in the html
Thanks,
Re: define( ) or not
Posted: Fri Feb 01, 2008 8:47 am
by Maugrim_The_Reaper
It depends, I think using variables is more flexible since you can change their value from a set of default, by including more specific value sets on a per app, per language, basis. Using constants makes that almost impossible.
As for performance - both are quite efficient, and I doubt the difference would have a significant impact on your application.
Re: define( ) or not
Posted: Fri Feb 01, 2008 9:58 am
by pickle
I usually define constants for values I'm sure I don't want to change throughout the application. It also has the advantage of setting a variable that I don't have to worry about passing. In your examples, setting a constant with define() allows you to use that variable absolutely anywhere, whereas setting it traditionally means you'd have to pass that variable around.
If you're currently set up to use define(), I'd say continue. Do make sure though that all your define() statements are in one place, as it'll be hell to find the value of a particular constant if you've got define() sprinkled throughout your code.
Re: define( ) or not
Posted: Fri Feb 01, 2008 12:11 pm
by kryles
yah I always have one file specifically for constants (creatively called constants.php

)
Yah they are labels to identify textboxes so I don't see them changing, so I'll go with defines if there is not a noticeable performance issue.
Thanks all

Re: define( ) or not
Posted: Mon Feb 04, 2008 8:16 pm
by Benjamin
I ran tests on this, if I remember correctly defining constants uses less memory and is faster than setting variables or arrays.
Re: define( ) or not
Posted: Mon Feb 04, 2008 10:01 pm
by Christopher
kryles wrote:I'm just wondering if using define() a lot takes up many resources? For instance I'm making fields for a registration page as such:
I'm not asking about the code syntax, I would just like to know if define is a lot more greedy than simply doing
Or if I'm going overkill and I should just type *Last Name: directly in the html
I'll be the one to ask the Premature Optimization questions: Are you running out of "resources" (I am guessing either CPU or memory)?
You should use defines for what they are intended for, which is to provide read-only values for your scripts. If you are having performance problems then switching var to defines, or back, will hardly be among the solutions.
Re: define( ) or not
Posted: Mon Feb 04, 2008 10:01 pm
by kryles
I guess that might be a reasonable assumption. The php will already know how much memory to set aside, whereas with variables it has to calcualte on the fly....maybe?
No performance issues, was more just wanting to code using good practices.
Re: define( ) or not
Posted: Mon Feb 04, 2008 10:09 pm
by Christopher
PHP will allocate and free all memory on each request. The idea behind avoiding Premature Optimization is that you should focus first on good design and then optimize later as needed. The reason is that optimizing early can narrow your design options and in the long run can produce both a poorer design and poorer performance. All this does not mean that you should not be aware of performance issues; but they are usually contained in best practices anyway.