define usage

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have seen numerous examples of the use of the define() function
online.  However, I also notice that define returns true or false
depending on success or failure in defining the constant.  I can't
find any documentation on what conditions define will fail.  But at
the same time nobody ever checks the output of this function in their
programs.  In what cases would define() not work?  Why doesn't anybody
check the output of this function?  Is there some reason why so many
programmers rely on this function *never* failing or perhaps this is
just bad programming practice?  In some programs I would assume they
would fundamentally fail if define() failed to define an important
constant their program depended upon.

Re: define usage

On Mar 24, 2:00 pm, wrote:
Quoted text here. Click to load it

It would fail if you try to assign a new value to your previously
defined constant (since constants are immutable):

define ('test', 'first');
if (!define('test', 'second')) {
  echo "Error!";
echo test;

Why people don't check?  Perhaps they think they are masters of their
global namespace?  Perhaps they don't care if their second assignment
fails?  Perhaps they program with error reporting turned off?

I _rarely_ use global constants and my opinion is that they shouldn't
be used as much as possible.  Really are much better solutions,
especially if you're programming OOP.

Re: define usage

ELINTPimp wrote:
Quoted text here. Click to load it

Actually most programmers will program with error reporting turned on
and therefore see the error.

The only reason I know of that a define() would fail is if the constant
is already defined and you would catch that during development.

Quoted text here. Click to load it

Everyone's entitled to their own opinion, however either back it up with
your 'better solutions' or don't give the advice to avoid constants.

Constants are very valuable in programming, more than anything *because*
they can't be changed once they have been defined.
If used sensibly, this in effect means that your constants will never be
You can trust the data contained in constants which is something which
can not be said about variables, especially global variables.

Re: define usage

Quoted text here. Click to load it

This seems to be an opinion as well, and you do not provide
statistical evidence of this fact.  Yes, best practice dictates using
error reporting during development, but from experience I've seem many
scripts I've been hired to refactor that evidence proves otherwise.

Quoted text here. Click to load it

I agree, since I said this too.

Quoted text here. Click to load it

I prefaced this as my opinion for this reason, I wished to share my
opinion so the individual could ask why if interest, not if they
aren't and therefore save time in the process.  But, since I'm in a
rare mood, I will.

Constants are attractive due to their immutable and global scope, as I
previously stated.  The first problem that I personally have is just
how attractive they become to programmer, new and sometimes seasoned
(usually with time constraints) alike.  In complex software
applications, these global constants can get you into trouble in
namespace collisions, sometimes causing unknown errors (when
programmed without E_STRICT on) and unpredictable (and often hard do
discover) results.

My second reason for being hesitant for constants is that when a
programmer is using a constant, it is often something that would be
best provided in a configuration file to keep your application highly
configurable.  For example, I have (often) seen tax rates as constants
before, and that's great...until the tax rate changes.  More on this
in a moment...

Now, I also said "Really are much better solutions, especially if
you're programming OOP." and most of my PHP and otherwise programming
are for enterprise solutions using OOP methodology.  If I require
something constant-like, I weigh using class constants (if this is
really the best solution) where the scope is limited to the class.  If
I need something of global scope, I will open employ a Registry
pattern to cleanly store this information.  If I am pulling items from
a configuration file, which I previously mentioned, I will create a
Registry pattern with method visibilities that restrict changing the
variable values.  For example, I could have the constructor call a
public method that reads the configuration file and sets the class
values, but no methods to change any/all/some of the values during
runtime (the public method in the event I want to reload the
configuration file for some reason...such as if I have this info
cached and I it becomes flagged for a refresh).

I feel constants are an important construct, and critical to some
applications, but should be used sparingly and with skepticism...a
possible code smell.  The reason I left my initial response as it was
was for the requesters benefit, as it seemed that they might be new to
PHP and I didn't know how much was too much.



Re: define usage

Quoted text here. Click to load it

Only scalar values can be defined as constants
(integer,string,float,bool) so


would fail

Site Timeline