在PHP中使用define/apc_define_constants取消定义常量 [英] Un-define constants with define/apc_define_constants in PHP

查看:67
本文介绍了在PHP中使用define/apc_define_constants取消定义常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用常量来输出不同语言的消息.例如,如果用户选择英语",则需要具有该常数的文件:

  define('welcomeMessage','Welcome!'); 

如果她选择西班牙语":

  define('welcomeMessage','Bien Venidos!'); 

等等等...

当用户遍历各种语言时,会发生此问题.我无法使用define/apc_define_constants重新定义常量(据我所知).我可以删除并重新定义它们吗?

对此有一个好的解决方案吗?

解决方案

常量使用 define() 创建创建后就不能取消定义.

使用 apc_define_constants()创建的常量

可以通过向函数传递一个空数组来删除code> .

我不确定我为什么理解这是一个问题.您所说的用户遍历各种语言"是什么意思?一旦请求返回给用户并且他们生成新请求(通过重定向,提交表单,单击链接或生成AJAX请求),您就可以在新调用中自由定义所需的任何常量./p>

除非问题是定义常量,然后调用代码以设置新的语言/消息,否则将触发尝试设置所有常量的操作(这将失败,并出现 define())

I'm using constants for output messages in different languages. For example, if a user chooses "English", a file with this constant would be required:

define('welcomeMessage','Welcome!');

If she chooses "Spanish":

define('welcomeMessage','Bien Venidos!');

etc etc...

The problem occurs when a user iterates through languages. I can't redefine constants with either define/apc_define_constants (as far as I know). Can I delete and redefine them?

Is there a good solution for this?

解决方案

Constants created with define() can't be undefined once created.

Constants created with apc_define_constants() can be removed by passing an empty array to the function.

I'm not sure I understand why this is a problem however. What do you mean "the user iterates through languages"? As soon as a request returns to the user and they generate a new request (via a redirect, submitting a form, clicking a link or generating an AJAX request) then you're free to define whatever constants you like on the new invocation.

Unless the problem is that you define the constants and then call the code to set the new language/messages, which triggers an attempt to set all the constants (which will fail with define()).

这篇关于在PHP中使用define/apc_define_constants取消定义常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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