Re: [Strawpoll] Promoting redefining constant to exception
| From: | Rowan Tommins | Date: | Tue, 25 Jan 2022 22:11:03 +0000 |
| Subject: | Re: [Strawpoll] Promoting redefining constant to exception | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116924@lists.php.net to get a copy of this message | ||
On 25/01/2022 19:44, Chase Peeler wrote:
If we start throwing exceptions, which can't be suppressed, it will make
this much more difficult to read since the constants.php will have to be
updated:
if(!defined('dbserver')){
define('dbserver','productiondb.example.com');
}
if(!defined('otherconstant')){
define('otherconstants','foobar');
}
A wrapper function for this use case is entirely trivial:
function define_if_not_defined($name, $value) {
if ( ! defined($name) ) {
define($name, $value);} } Give it a short name like 'def', and your config file actually becomes shorter: def('dbserver', 'productiondb.example.com'); def('otherconstants', 'foobar'); "define" itself is just a function name, not a reserved word, so you could even declare the above as "define" in a namespace, then add "use function SomeNamespace\define;" and leave the rest of the file alone. Regards, -- Rowan Tommins [IMSoP]