note 59274 deleted from function.define by tularis
| From: | tularis@php.net | Date: | Sun, 03 Jun 2007 09:40:14 +0000 |
| Subject: | note 59274 deleted from function.define by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127151@lists.php.net to get a copy of this message | ||
Note Submitter: richard dot quadling at bandvulc dot co dot uk
----
In trying to remove magic numbers from my code, I often use sets of defines. Some sets get quite
big.
I sort my defines so that they are alphabetically arranged - for me this is easier.
The issue now comes as to what is the next available number or what happens to the number sequence
if I remove a define.
My solution is to use the following mechanism.
<?php
$iRuleBase = 0;
define ('RULE_CALLBACK_FORMAT', ++$iRuleVal);
define ('RULE_CHANGE_CALLBACK_ON_ERROR', ++$iRuleVal);
define ('RULE_CHANGE_COMPARE_DATE', ++$iRuleVal);
define ('RULE_CHANGE_OLD_COLUMN', ++$iRuleVal);
define ('RULE_CHANGE_ON_DATE', ++$iRuleVal);
define ('RULE_DESC', ++$iRuleVal);
define ('RULE_EXPECTED_RESULT', ++$iRuleVal);
define ('RULE_LIMIT_TO_PERIOD', ++$iRuleVal);
define ('RULE_MATCH_ARRAY', ++$iRuleVal);
define ('RULE_MATCH_COLUMN', ++$iRuleVal);
define ('RULE_MESSAGE', ++$iRuleVal);
define ('RULE_REGEX', ++$iRuleVal);
?>
I no longer need to know ANY of the magic numbers. I can add and remove them at will and the code
will show errors for the missing ones as the constant will be undefined. Much better than using the
number and forgetting what entry 6 is.
You also have the value of $iRuleVal to indicate the next define if needed.
The defines then work like ...
<?php
$aGlobalValidationDefinitions = array(
'CH_SORT' => array
(
RULE_DESC => 'Contract on Cost Header / Project must match the Contract on the
order.',
RULE_MATCH_COLUMN => 'POH_CONTRACT',
),
'CH_STATUS' => array
(
RULE_CHANGE_CALLBACK_ON_ERROR => 'dataentryValidateChanges',
RULE_CHANGE_COMPARE_DATE => 'POH_DATE_AUTH',
RULE_CHANGE_OLD_COLUMN => 'OLDSTATUS',
RULE_CHANGE_ON_DATE => 'VSC_DATECHANGED',
RULE_DESC => 'Vehicle Status must be A - Active or R - Reopened.',
RULE_LIMIT_TO_PERIOD => 'D',
RULE_REGEX => '^(?=[AR])(.)$',
),
'POD_COSTHEADER' => array
(
RULE_DESC => 'Must not start with "UNKNOWN".',
RULE_REGEX => '^(?!UNKNOWN)(.*)$',
),
);
?>
No magic numbers. You can add or remove defines without ever worrying about breaking the code and
you never have 2 defines with the same value!