本文介绍了在 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.

解决方案

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 中重新定义常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:02