note 56756 modified in language.constants by danbrown

From: Date: Tue, 26 Jul 2011 19:34:25 +0000
Subject: note 56756 modified in language.constants by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181908@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: <?php 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 : <?php 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 : <?php 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 ) --was-- 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 ) http://php.net/manual/en/language.constants.php

« previous php.notes (#181908) next »