PHP 常量不存在:注意

发布于 2024-07-12 05:01:01 字数 342 浏览 8 评论 0原文

我想在我的 PHP 应用程序中使用常量作为配置变量,只有当常量不存在(无论出于何种原因)时,它才会向我发出通知,但我希望这是一个错误(或异常),就像当我尝试回显一个不存在的变量时。

是否可以在不使用单独的函数来获取常量值的情况下实现这一点,如下所示:

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

进行了一些搜索,但找不到任何东西。

对我来说似乎很合乎逻辑,当我尝试使用不存在的变量时,代码执行立即退出并抛出错误,为什么不使用常量呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一个人的旅程 2024-07-19 05:01:01

我建议使用 define 与它你应该能够做类似的事情

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

编辑:

正如我在下面的评论中所述,使用此方法的替代方法是使用自定义错误处理程序,方法是使用 set_error_handler 您应该能够捕获通知并采取行动。

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.

冬天旳寂寞 2024-07-19 05:01:01

您可以创建自定义错误处理程序

  1. 检查错误是否是通知
  2. 检查错误是否为 通知字符串包含“未定义的类常量”
  3. 如果这两个计数都是如此,则抛出异常

这将得到您想要的。 这种方法的问题是您现在负责处理所有错误。 PHP 的错误处理将被完全绕过。

另一种方法是定义一个配置类,并使用它的常量,

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

而不是发出通知,您会得到一个致命错误,这似乎更正确。 不幸的是,如果没有类似的恶搞,您就无法捕获致命错误(请参阅手册的 set_error_handler 条目中的注释)。

最后一个选项是创建一个配置单例,其中私有类变量保存您的值,这与我们开始的地方相去甚远。 这使您可以完全编程控制当有人尝试访问未定义的配置值时会发生什么,但您确实失去了常量的好处。

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.

贪恋 2024-07-19 05:01:01

我建议您安装一个错误处理程序,将所有错误(无论级别如何)转换为异常。 您的代码不应有任何错误,无论是警告还是通知。

请参阅:handling-errors-as-exceptions-best-methods

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

猫卆 2024-07-19 05:01:01

使用define函数检查是否存在给定名称的常量。

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

━╋う一瞬間旳綻放 2024-07-19 05:01:01

是的,我知道定义的函数,但是每次检查常量是否存在是一项相当艰巨的工作,所以我正在寻找某种 php 设置或技巧,而不使用额外的函数来自动抛出错误或异常当常量不存在时的通知。

对我来说似乎很合乎逻辑,当我尝试使用不存在的变量时,代码执行立即退出并抛出错误,为什么不使用常量呢?

到目前为止谢谢(-:

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 (-:

抹茶夏天i‖ 2024-07-19 05:01:01

我遇到了同样的问题,我必须捕获错误的唯一方法是:

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

if (define('CONSTANT')) { ... 对我不起作用!

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!

笑叹一世浮沉 2024-07-19 05:01:01

如果您在同一个类中,您可以使用它来访问常量,

self::CONSTANT

基本上添加 self::

如果您在另一个类中,请使用类名来访问它

EXAMPLE::CONSTANT

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文