note 59274 added to function.define
| From: | richard dot quadling at bandvulc dot co dot uk | Date: | Thu, 01 Dec 2005 11:40:35 +0000 |
| Subject: | note 59274 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99412@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.define.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59274
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59274&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59274&report=yes
Search -- http://master.php.net/manage/user-notes.php