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()).