Redefine constant

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Redefine constant

Post by shiznatix »

I know this is crazy because as its said if you can redifine a constant it wouldn't be a constant but is there a way to remove a certain number of constants from memory and thus redefine them that way? Basically here is my problem:

I have all of my translations in php files that are just a large amount of constants. Stuff like:

Code: Select all

define('SIGNUP_EMAIL_BODY', 'body this baby cakes!');
define('SIGNUP_EMAIL_FOOTER', 'dont put your feet on my pillow crazy guy);
and I will save that one as signup-email.en.php. Then if the user is browsing the site in Swedish I just load signup-email.sv.php and that file just has stuff like:

Code: Select all

define('SIGNUP_EMAIL_BODY', 'swedish translation of what i put in english here (you get the idea');
this situation has worked really well for me for like a year but now I have come into a bit of trouble. Basically I am trying to send out mass emails to new users. I have the language these users have been browsing in saved in the DB and so I just get all the new users and their language and cycle through them and send the email in their language. Now the problem is that if this list contains users who have a different language saved I will get the "constant already defined..." error. What I want to do is after each email is sent remove the constants that are used in the email so I can redefine them to send out an email in a different language without getting these errors.

The only other option I can think of is to just do this 1 by 1 which would take forever. I can't just do it by language, I would explain why but it would take forever to explain the whole real system, what I gave you was just a condensed version that reflects my basic problem. Is there any way to do this?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Redefine constant

Post by Mark Baker »

shiznatix wrote:I know this is crazy because as its said if you can redifine a constant it wouldn't be a constant but is there a way to remove a certain number of constants from memory and thus redefine them that way?
The answer is not to use constants

If you really, absolutely have to redefine constants mid-script, then look to the the runkit_constant_redefine() or runkit_constant_remove() functions in the runkit extension



Note also that constants are inefficient. You might be better off creating a series of class files with class constants, and using those instead
Post Reply