This content is a little crusty, having been with me through 3 separate
platform changes. Formatting may be rough, and I am slightly less
stupid today than when I wrote it.
Circumvent PHP errors with define_once()
Core PHP does not include a define_once()
function to complement functions like require_once()
and include_once()
, which is pretty silly in my opinion. While I am generally not a fan of using *_once
statements due to the performance penalty (and incurred laziness), define_once
is the exception. There are ways to look for a loaded/missing file, but a define is not a define until you define it, so you really have no choice.
So in situations where you have to blindly load defines — I do it to build language defines in a cascading templating system — use this function to achieve the proper results:
function define_once($define, $value) {
if (!defined((string)$define)) {
define($define, $value);
return true;
}
return false;
}
```
comments powered by Disqus