在 PHP 中使用 Define/apc_define_constants 取消定义常量
我使用常量来输出不同语言的消息。 例如,如果用户选择“英语”,则需要具有此常量的文件:
define('welcomeMessage','Welcome!');
如果她选择“西班牙语”:
define('welcomeMessage','Bien Venidos!');
等等等等...
当用户迭代语言时就会出现问题。我无法使用 Define/apc_define_constants 重新定义常量(据我所知)。我可以删除并重新定义它们吗?
对此有好的解决办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果值以后可以更改,那么使用常量就没有意义。我建议创建一个静态类,您可以在其中设置语言,而不是使用常量,您将从该类中收到欢迎消息。假设该类名为 Lang:
getWelcome() 方法检查使用 setLang() 设置的 lang 值并返回适当的翻译字符串。
使用静态类意味着您不必实例化该类,并且所有其他代码都可以引用该静态类,而无需创建新实例并设置所使用的语言。
It doesn't make sense to use a constant if the value can be changed later. I would recommend creating a static class where you can set the language and instead of using constants you would get your welcome message from that class. Let's say the class was named Lang:
The getWelcome() method checks the lang value set with setLang() and returns the appropriate translated string.
Using a static class means you won't have to instantiate the class, and all other code can reference that static class without having to make new instances and having to set the language being used.
常量 使用
define()
一旦创建就不能取消定义。使用
apc_define_constants()
< 创建的常量/a> 可以通过将空数组传递给函数来删除。但我不确定我是否理解为什么这是一个问题。 “用户迭代语言”是什么意思?一旦请求返回给用户并且他们生成新请求(通过重定向、提交表单、单击链接或生成 AJAX 请求),您就可以在新调用中自由定义您喜欢的任何常量。
除非问题是您定义了常量,然后调用代码来设置新的语言/消息,这会触发设置所有常量的尝试(这将因
define()
而失败)。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()
).我不知道用户如何在运行时更改其语言,但不应在运行时修改或重新定义常量。
考虑使用全局变量。
I don't know how the user changes his language during runtime but constants should not be modified or redefined during runtime.
Consider using global variables.
您不应该将您的语言定义为常量。还有更灵活的方法,例如将它们放入语言类、数据库甚至本地文件中。将它们放在代码中是很糟糕的。
语言类应该抽象掉所有细节。
传递给语言构造函数的参数仍然可以是定义,以及 get() 参数,但实际的语言文本不应该存在于代码中,我会将其粘贴在数据库中。
You should not have your languages defined as constants. There are far more flexible ways, like putting them in a language class, the database or even local files. Having them in code is just bad.
The language class should abstract away all the details.
The parameters passed to the language constructor could still be a define, along w/the get() arguments, but the actual language text should not live in code, I'd stick it in the database.
当我需要需要重新定义的“常量”时,我会这样做(听起来很有趣):
现在,每当您需要定义“常量”时,只需使用:
当您需要访问它时,请使用:
如果您需要检查它是否已经被定义,使用:
为什么我使用这个?因为它看起来像我的代码中的define()函数。这使得更容易将设置与变量分开。我主要在部分视图中使用它,其中我需要一个具有全局范围的变量(如常量),但能够随时重新定义它。
希望对您有帮助。
Here is what I do when I need "constants" that need to be redefined (as funny as it sounds):
Now whenever you need to define your "constant" just use:
When you need to access it, use:
If you need to check if it's been defined already, use:
Why I use this? Because it looks like define() function in my code. That makes it easier to separate settings from variables. I mainly use it in my partial views where I need to have a variable with global scope (like constants) but be able to redefine it at any time.
Hope it helps you.