note 72069 deleted from function.define by cmb
| From: | cmb@php.net | Date: | Sun, 06 Oct 2019 07:56:29 +0000 |
| Subject: | note 72069 deleted from function.define by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212486@lists.php.net to get a copy of this message | ||
Note Submitter: stangelanda at arrowquick dot com
----
An improvement on the function from bobbykjack at yahoo dot co dot uk on the concept by richard dot
quadling at bandvulc dot co dot uk:
<?php
function adefine($constant_name, $value=null) {
static $increment = 0; // 1 for bitmask
if (is_null($value)) {
define($constant_name, ++$increment); // $increment=$increment<<1 for bitmask
} else {
define($constant_name, $value);
if (is_numeric($value)) {
$increment = $value;
}
}
}
?>
If you pass it a second argument it defines it normally, and resets the increment if the value is
numeric. This way the function can replace define, and you can reset the counter for a new set of
constants.
<?php
adefine ('RULE_CALLBACK_FORMAT', 1); // 1
adefine ('RULE_CHANGE_CALLBACK_ON_ERROR'); // 2
adefine ('RULE_CHANGE_COMPARE_DATE'); // 3
adefine('KEYWORD', 'hodgepodge'); // hodgepodge <-- defined normally
adefine ('RULE_CHANGE_ON_DATE'); // 4
adefine ('ERROR_DESC', 1); // 1 <-- Counter reset
adefine ('ERROR_EXPECTED_RESULT'); // 2
?>