views:

493

answers:

6

Hi,

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

Is that possible without using a seperate 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 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 Stefan

+7  A: 

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.

Mark Davidson
A: 

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

Gumbo
A: 

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
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
@Mark, post a reposnse. That's a good solution.
Typeoneerror
+1  A: 

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

troelskn
+1  A: 

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.

Alan Storm
+1  A: 

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