在 PHP 中重新定义常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8465155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redefining constants in PHP
提问by abuduba
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
函数定义的 php 中重新定义常量?我有一个包含多个常量的类,其中包含用户数据。我正在尝试为多个用户使用该类。
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.
我没有写这个类,但由于时间限制我正在使用它。我不知道为什么这个类的创建者defined
使用用户数据而不是实例中的变量。
I know that constants should be constant (obviously!), but I'm looking for a trick to redefine them.
我知道常量应该是常量(显然!),但我正在寻找重新定义它们的技巧。
回答by Tim Cooper
If you have the runkit extensioninstalled, you can use runkit_constant_redefine()
:
如果您安装了runkit 扩展,则可以使用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.
然而,在大多数情况下,重新评估为什么使用常量而不是更合适的东西会是一个更好的主意。
回答by ajreal
No, you cannot redefine a constant (except with runkit_constant_redefine),
that's why is called CONSTANT.
不,你不能重新定义一个常量(除了runkit_constant_redefine),
这就是为什么被称为常量。
What you are looking for in the class is actually an object variable in array format:-
您在类中寻找的实际上是数组格式的对象变量:-
class user
{
public $user = array();
function load($user_id)
{
// etc
$this->$user[$user_id] = something_else;
}
}
回答by Dereckson
In PHP 5.6+, you can import constants, under an alias. Such alias can overwrite an already existing constant.
在 PHP 5.6+ 中,您可以在别名下导入常量。这样的别名可以覆盖已经存在的常量。
The following snippet will print int(5)
:
以下代码段将打印int(5)
:
define('ALLEGRO_ID', 3);
define('NEW_ALLEGRO_ID', 5);
use const NEW_ALLEGRO_ID as ALLEGRO_ID;
var_dump(ALLEGRO_ID);
Such mechanism could be legitimate in unit test, or other areas where magic behavior is tolerated, but isn't convenient for your use case, where a class to store your user settings would be more useful.
这种机制在单元测试或其他可以容忍魔术行为的领域中可能是合法的,但对于您的用例来说并不方便,在这种情况下,存储用户设置的类会更有用。
Finally, this behavior allows to create very confusing situations like use const true as false;
.
最后,这种行为允许创建非常混乱的情况,例如use const true as false;
.
回答by atif
Constants value once defined remains unchanged throughout the program. If you have such a requirement where you want to change the values in future then keep such values in PHP variables.
常量值一旦定义就在整个程序中保持不变。如果您有这样的需求,希望将来更改这些值,请将这些值保留在 PHP 变量中。
回答by Ronak Dhoot
passing 3rd param as true will help you
将第三个参数传递为 true 会帮助你
define('CONSTANT_NAME', 'constant_value', true);
print CONSTANT_NAME.PHP_EOL;
define('CONSTANT_NAME', 'constant_value2');
print CONSTANT_NAME.PHP_EOL;