This is from the php manual: http://us.php.net/manual/en/language.constants.syntax.php
If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens.
I really don't like this behavior. If I have failed to define a required constant, I would rather the script fail so that I am forced define it. Is there any way to force PHP to crash the script if it tries to use an undefined constant?
For example. Both of these scripts do the same thing.
<?php
define('DEBUG',1);
if (DEBUG) echo('Yo!');
?>
and
<?php
if(DEBUG) echo('Yo!');
?>
I would rather the second script DIE and declare that it tried to use an undefined constant DEBUG.
You could do something (ugly) like this:
pseudo code:
/**
* A Notice becomes an Error :)
*/
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_NOTICE) { // = 8
if (substr($errstr ... )) { // contains something which looks like a constant notice...
trigger_error('A constant was not defined!', E_USER_ERROR);
}
}
}
set_error_handler("myErrorHandler");
set_error_handler()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With