Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

Redefining constants in PHP

Is it possible to redefine a constant in php which was defined by the define function? I have a class with several constants that contains the user data. I'm trying to use the class for more than one user.

define('ALLEGRO_ID', 'id');
define('ALLEGRO_LOGIN', 'login');
define('ALLEGRO_PASSWORD', 'passwd');
define('ALLEGRO_KEY', 'key');
define('ALLEGRO_COUNTRY', 123);

$allegro = new AllegroWebAPI( );                
$allegro -> Login();

I did not write this class, but I am using it due to time constraints. I do not know why the creator of this class defined the user data rather than using the variables in the instance.

I know that constants should be constant (obviously!), but I'm looking for a trick to redefine them.

question from:https://stackoverflow.com/questions/8465155/redefining-constants-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have the runkit extension installed, you can use runkit_constant_redefine():

runkit_constant_redefine("name", "value");

In most circumstances, however, it would be a better idea to re-evaluate why you're using constants and not something more fitting.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...