PHP 字符串常量过度使用?
我有两个特殊情况,我不同意同事是否应该使用常量。
我们使用一个自制的框架,工作方式大致类似于 Symfony 1.x。
初始代码位于用于路由的 PHP 路由配置文件中,如下所示:
$router->map('/some_url', array('module' => 'some_module', 'action' => 'some_action')); $router->map('/some_other_url', array('module' => 'some_module', 'action' => 'some_action')); // ETC。
同事将其更改为:
$router->map('/some_url', array(MODULE => 'some_module', ACTION => 'some_action')); $router->map('/some_other_url', array(MODULE => 'some_module', ACTION => 'some_action')); // + 在constants.php 文件中: 定义('模块','模块'); 定义('行动','行动');
在我看来,这是不断的过度使用:如果“模块”或“动作”的概念被重命名,则必须在整个代码中重命名它,无论是写为字符串还是常量。另外,上面定义的常量名称没有特定的含义,有利于命名冲突/混淆。
初始代码示例:
if (isset($_SESSION['unid']) && isset($_SESSION['login'])) { ... }
由同事修改:
if (isset($_SESSION[UNID]) && isset($_SESSION[LOGIN])) { ... } // + 在constants.php 文件中: 定义('UNID','unid'); 定义('登录','登录');
在我们的应用程序中,这些会话变量名称
unid
和login
显然不太可能更改。尽管如此,如果在这里声明常量确实是一个好的做法,我建议至少更精确的名称,例如FIELDNAME_UNID
和FIELDNAME_LOGIN
...
引入这些常量确实相关吗(也就是说,命名应该得到改进),或者(据我猜测)完全无用?
谢谢。
编辑
几个月后,这里有来自 constants.php 文件的几行(令人难以置信的)内容。 我确实发现这是完全无用的混乱,类似于这篇 DailyWTF 帖子。太多的常量会杀死常量。
define('POST', 'POST');
define('GET', 'GET');
define('PROJECT', 'project');
define('APPLICATION', 'application');
define('MODULE', 'module');
define('ACTION', 'action');
define('ID', 'id');
define('SLUG', 'slug');
define('CONTROLLER', 'controller');
define('CONTENT', 'content');
define('AJAX', 'ajax');
define('EXECUTE', 'execute');
define('FORMAT', 'format');
define('BASE_HREF_CONSTANT', 'basehref');
define('UNID', 'unid');
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('TEMPLATE', 'templates');
define('UNSECURE', 'unsecure');
define('MODE', 'mode');
define('MESSAGE', 'message');
define('TEMPORARY_SESSION', 'temporary_session');
define('ERRORMESSAGE', 'errormessage');
define('START_FROM', 'startfrom');
define('COUNT', 'count');
// and so on.
I have two particular cases where I disagree with a coworker, whether constants should be used or not.
We use a homemade framework working roughly like Symfony 1.x.
Initial code was, in a routing PHP config file for routing, like this one:
$router->map('/some_url', array('module' => 'some_module', 'action' => 'some_action')); $router->map('/some_other_url', array('module' => 'some_module', 'action' => 'some_action')); // etc.
The coworker changed it to:
$router->map('/some_url', array(MODULE => 'some_module', ACTION => 'some_action')); $router->map('/some_other_url', array(MODULE => 'some_module', ACTION => 'some_action')); // + in constants.php file: define('MODULE', 'module'); define('ACTION', 'action');
IMO this is constant overuse: if the concept of "module" or "action" is ever renamed, it would have to be renamed in the entire code, either written as a string or a constant. Plus, the defined constant names above have no specific meaning, favoring naming collisions/confusions.
Initial code example:
if (isset($_SESSION['unid']) && isset($_SESSION['login'])) { ... }
Modified by the coworker:
if (isset($_SESSION[UNID]) && isset($_SESSION[LOGIN])) { ... } // + in a constants.php file: define('UNID', 'unid'); define('LOGIN', 'login');
In our application, those session vars names
unid
andlogin
are clearly unlikely to change. Nonetheless, if declaring constants was really a good practice here, I would suggest at least more precise names, for exampleFIELDNAME_UNID
andFIELDNAME_LOGIN
...
Is introducing those constants really relevant (that is, naming should just be improved), or (as I guess) completely useless ?
Thanks.
EDIT
After a few months, here are a few (incredible) lines from the constants.php file.
I definitely find this a completely useless mess, similar to this DailyWTF post. Too many constants kills constants.
define('POST', 'POST');
define('GET', 'GET');
define('PROJECT', 'project');
define('APPLICATION', 'application');
define('MODULE', 'module');
define('ACTION', 'action');
define('ID', 'id');
define('SLUG', 'slug');
define('CONTROLLER', 'controller');
define('CONTENT', 'content');
define('AJAX', 'ajax');
define('EXECUTE', 'execute');
define('FORMAT', 'format');
define('BASE_HREF_CONSTANT', 'basehref');
define('UNID', 'unid');
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('TEMPLATE', 'templates');
define('UNSECURE', 'unsecure');
define('MODE', 'mode');
define('MESSAGE', 'message');
define('TEMPORARY_SESSION', 'temporary_session');
define('ERRORMESSAGE', 'errormessage');
define('START_FROM', 'startfrom');
define('COUNT', 'count');
// and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
拼写错误常量的优点
缺点
Advantages
Disadvantages
使用这样的常量是有道理的。
如果你不小心做了类似的事情:
它将以某种未定义的方式失败(注意拼写错误的“moduel”)。
如果在涉及常量时犯了拼写错误或拼写错误,PHP 会发出一个通知,您可以立即捕获它。
这实际上能拯救你多久是一个有争议的问题。就我个人而言,我通常认为不值得这么麻烦。
There is a valid argument for using constants like that.
If you accidentally do something like:
it will fail in some undefined way (note the misspelled "moduel").
If you make a spelling mistake or typo when a constant is involved, PHP emits a Notice, and you catch it right away.
How often that actually saves you is a matter for debate. Personally, I usually don't think it's worth the trouble.
当然,主要原因是 IDE 自动补全。
使用常量,您甚至不需要编写常量的全名(IDE 将帮助您),并且您将绝对确定没有拼写错误。
这不是过度使用。例如,我试图避免在代码中使用字符串。
And main reason is IDE autocompletion, of course.
With using constants you will not even need to write full name of constant (IDE will help you in this) and you will be absolutely sure that you have no typos.
It's not overuse. For example, I'm trying to avoid using strings in code.