3

I'd like to use constants as config variables within my PHP applications, only when a constant doesn't exist (for whatever reason) it throws me a notice but I'd like that to be an error (or exception), just like when I try to echo a not existing variable.

Is that possible without using a separate function for getting constant values, something like this:

echo $non_existing_variable; // Error
echo NON_EXISTING_CONSTANT; // Now also an error

Been searching around a bit but couldn't find anything.

Seems kind of logical to me, when I try to use a variable that doesn't exist code execution quits immediately and throws an error, why isn't that with constants?

Rodia
  • 1,407
  • 8
  • 22
  • 29

8 Answers8

11

I'd suggest using defined with it you should be able to do something like

if (defined('CONSTANT')) {
    echo CONSTANT;
} else {
    throw new Exception('Undefined Constant.');
}

Edit:

The alternative to using this method as I stated in the comment below is to use a custom error handler, by using set_error_handler you should be able to catch the notice and take an action.

genesis
  • 50,477
  • 20
  • 96
  • 125
Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
2

You could create a custom error handler that would

  1. Check if the error was a Notice
  2. Check if the error string contained 'Undefined class constant'
  3. If so on both counts, throw your exception

This will get you what you want. The problem with this approach is you're now responsible for handling ALL errors. PHP's error handling will be completely bypassed.

Another approach would be to define a config class, and use its constants

class Config{
    const CONFIG_ONE = 42;
}
//will produce a fatal error 
if(Config::CONFIG_TWO){
   //...
}

Instead of a notice you'll get a fatal error, which seems more correct. Unfortunately, you can't catch fatal errors without similar hijinks (see comments in the manual's set_error_handler entry).

A final option, which is way off the beaten path from where we started is to create a configuration singleton, with private class variables holding you values. This gives you full programatic control over what happens when someone tries to access an undefined configuration value, but you do loose the benefits of constants.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
2

You might to write your own error handler. More info here:

http://php.net/manual/en/errorfunc.examples.php

http://www.w3schools.com/php/php_error.asp

waney
  • 402
  • 1
  • 5
  • 20
1

I'd suggest that you install an error-handler that turns all errors (regardless of level) into exceptions. Your code shouldn't have any errors, whether they are warnings or notices.

See: handling-errors-as-exceptions-best-methods

Community
  • 1
  • 1
troelskn
  • 115,121
  • 27
  • 131
  • 155
0

Use the defined function to check if there is a constant with the given name.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

Yes i know about the defined function, but to check every time if a constant exists is quite a work, so i'm looking for some kind of php setting or trick without using a extra function to automaticly throw an error or exception in stead of a notice when a constant doesn't exist.

Seems kind a logical to me, when i try to use a variable that doesn't exist code execution quits immediatly and throws a error, why isn't that with constants?

Thanks so far (-:

  • Whoever said PHP was logical? ;) This kind of annoying leniency is quite a common trait in the language. – Rob Jan 31 '09 at 15:28
  • One idea you might want to look at it is using set error handler http://uk2.php.net/manual/en/function.set-error-handler.php with this you should be able to catch the notice and take an action. – Mark Davidson Jan 31 '09 at 15:30
0

I had the same problem and the only way I had to catch the error was:

try {
   echo CONSTANT;
} catch(\Exception $e) {
   // do something else
}

if (defined('CONSTANT')) { ... does not work for me!

Mutatos
  • 1,675
  • 4
  • 25
  • 55
0

if you are within the same class you can use this to access the constant

self::CONSTANT

basically add the self::

if you are in another class use the class name to access it

EXAMPLE::CONSTANT
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17