note 56756 added to language.constants
| From: | kencomer at NOSPAM dot kencomer dot com | Date: | Wed, 14 Sep 2005 12:38:03 +0000 |
| Subject: | note 56756 added to language.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95163@lists.php.net to get a copy of this message | ||
Being a belt and suspenders person, when I use a constant to do flow control (i.e., using constants
to determine which version of a section of the program should be used), I always use something like:
if ( defined('DEBUG') && TRUE===DEBUG )
If you accidentally use DEBUG somewhere before it is defined, PHP will create a new constant called
DEBUG with the value 'DEBUG'. Adding the second comparison will prevent the expression
from being TRUE when you did not intentionally create the constant. For the constant DEBUG, this
would rarely be a problem, but if you had (e.g.) a constant used to determine whether a function was
created using case-sensitive comparisons, an accidental creation of the constant IGNORE_CASE having
the value 'IGNORE_CASE' could drive you up the wall trying to find out what went wrong,
particularly if you had warnings turned off.
In almost all code I write, I put this function definition in my configuration section:
if (!function_exists("debug_print")) {
if ( defined('DEBUG') && TRUE===DEBUG ) {
function debug_print($string,$flag=NULL) {
/* if second argument is absent or TRUE, print */
if ( !(FALSE===$flag) )
print 'DEBUG: '.$string . "\n";
}
} else {
function debug_print($string,$flag=NULL) {
}
}
}
Then, in my code, I'll sprinkle liberal doses of debug code like :
define("DEBUG_TRACK_EXAMPLE_CREATION",FALSE);
class Example extends Something {
__construct($whatever) {
debug_print( "new instance of Example created with
'$whatever'\n",DEBUG_TRACK_EXAMPLE_CREATION);
}
}
and :
debug_print("finished init.\n")
In the first case, I would not want to see that message every time I went into DEBUG mode, so I made
it a special case. The second case is always printed in DEBUG mode. If I decide to turn everything
on, special cases and all, all I have to do is comment out the "if" line in debug_print()
and presto magicko! It costs a little and gains a lot.
As another belt-and-suspenders aside, notice that, unlike most people, I put the language constant
(e.g.,TRUE, "string", etc.) on the left side of the comparison. By doing that, you can
never accidentally do something like
if ( $hard_to_find_error="here" )
because you always write it as
if ( "here"==$no_error )
or, if you got it wrong,
if ( "here"=$easy_to_find_parse_error )
----
Manual Page -- http://www.php.net/manual/en/language.constants.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56756
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56756&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56756&report=yes
Search -- http://master.php.net/manage/user-notes.php