将预定义常量定义为 [英] What to define Predefined Constants as

查看:122
本文介绍了将预定义常量定义为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库类,自动建立与数据库的连接,并做一些基本的输入过滤和什么。我正在设置一些预定义的常量来调整类方法的行为。我应该如何设置常量的值?因为值永远不会被引用或比较,直接但只在常量名的上下文中的值甚至是重要的?

I have a database class that automatically sets up a connection to the database and does some basic input filtering and whatnot. I am looking at setting some predefined constants to adjust the behavior of the class methods. What should I set the values of the constants as? Since the values will never be referred to, or compared, directly but only in the context of the constant name does the value even matter?

我遇到的一个策略是将一个常量设置为一个位值,以便可以使用按位运算符来组合常量。

One strategy I have come across is setting a constant to a bit value so that bitwise operators can be used to combine constants. In this case it doesn't look like that functionality will be necessary, but you never know.

通常我会收到像

Notice: Use of undefined constant CONSTANT_VALUE - assumed 'CONSTANT_VALUE'

这是否像字符串CONSTANT_VALUE或常量CONSTANT_VALUE一样处理常量?我应该将我的常量的值定义为相同名称的字符串来弥补这一点吗?这发生在我使用我知道在某一点定义的常数,如DOCUMENT_ROOT。

Is this treating the constant like the string 'CONSTANT_VALUE' or the constant CONSTANT_VALUE? Should I be defining the value of my constants as strings of the same name to compensate for this? This occurs when I am using constants I know are defined at some point, like DOCUMENT_ROOT.

我缺少一个更好的做法,这些?

Am I missing a better practice that either of these?

推荐答案

如果您收到该消息,所讨论的常量不是 定义的代码运行点,它被视为字符串'CONSTANT_VALUE'

If you are getting that message, the constant in question is not defined at the point where that code is running, and it is being treated as the string 'CONSTANT_VALUE'.

如果一组常量的值, re define是完全任意的,不需要位掩码,使用正整数的序列。

If the values of a set of constants that you're defining are completely arbitrary and do not need to be bitmaskable, use the sequence of positive integers.

这篇关于将预定义常量定义为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆