重新定义PHP中的常量 [英] Redefining constants in PHP
问题描述
是否可以在PHP中重新定义由define
函数定义的常量?
我有一个带有几个包含用户数据的常量的类.我正在尝试为一个以上的用户使用该类.
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();
我没有编写此类,但由于时间限制,我正在使用它.我不知道为什么该类的创建者defined
为什么要使用用户数据而不是在实例中使用变量.
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.
推荐答案
如果您具有 runkit扩展名安装后,您可以使用 runkit_constant_redefine()
:
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.
这篇关于重新定义PHP中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!