PHP 常量不存在:注意
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我建议使用 define 与它你应该能够做类似的事情
编辑:
正如我在下面的评论中所述,使用此方法的替代方法是使用自定义错误处理程序,方法是使用 set_error_handler 您应该能够捕获通知并采取行动。
I'd suggest using defined with it you should be able to do something like
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.
您可以创建自定义错误处理程序来
这将得到您想要的。 这种方法的问题是您现在负责处理所有错误。 PHP 的错误处理将被完全绕过。
另一种方法是定义一个配置类,并使用它的常量,
而不是发出通知,您会得到一个致命错误,这似乎更正确。 不幸的是,如果没有类似的恶搞,您就无法捕获致命错误(请参阅手册的 set_error_handler 条目中的注释)。
最后一个选项是创建一个配置单例,其中私有类变量保存您的值,这与我们开始的地方相去甚远。 这使您可以完全编程控制当有人尝试访问未定义的配置值时会发生什么,但您确实失去了常量的好处。
You could create a custom error handler that would
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
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.
您可以编写自己的错误处理程序。
更多信息在这里:
http://php.net/manual/en/errorfunc.examples。 php
http://www.w3schools.com/php/php_error.asp
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
我建议您安装一个错误处理程序,将所有错误(无论级别如何)转换为异常。 您的代码不应有任何错误,无论是警告还是通知。
请参阅: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
使用
define
函数检查是否存在给定名称的常量。Use the
defined
function to check if there is a constant with the given name.是的,我知道定义的函数,但是每次检查常量是否存在是一项相当艰巨的工作,所以我正在寻找某种 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 (-:
我遇到了同样的问题,我必须捕获错误的唯一方法是:
if (define('CONSTANT')) { ... 对我不起作用!
I had the same problem and the only way I had to catch the error was:
if (defined('CONSTANT')) { ... does not work for me!
如果您在同一个类中,您可以使用它来访问常量,
基本上添加 self::
如果您在另一个类中,请使用类名来访问它
if you are within the same class you can use this to access the constant
basically add the self::
if you are in another class use the class name to access it