In the php 5.3 I can use class name as variable and I can call static variable.
在php 5.3中我可以使用类名作为变量,我可以调用静态变量。
$class_name = 'Test';
$class_name::$static_var;
How to call it in the php 5.2 version?
如何在php 5.2版本中调用它?
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ...
3 个解决方案
#1
1
T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - :: You can try this $class_name = 'Test'; $class_name->$static_var;
T_PAAMAYIM_NEKUDOTAYIM是PHP使用的双冒号范围解析 - ::你可以尝试这个$ class_name ='Test'; $ CLASS_NAME - > $ static_var;
#2
1
@user762799 here is the solution for what you want to do it in php 5.2
@ user762799这里是你想在php 5.2中做到的解决方案
class Sample{
public static $name;
public function __construct(){
self::$name = "User 1";
}
}
$sample = new Sample();
$class = 'Sample';
$name = 'name';
$val_name = "";
$str = '$class::$$name';
eval("\$val_name = \"$str\";");
//echo $val_name."<br>";
eval("\$name = $val_name;");
echo $name;
PAAMAYIM_NEKUDOTAYIM means scope resolution operator(::) actually in your code PHP is unable to identify $static_var
in the scope of $class_name
that is why the error occured.
PAAMAYIM_NEKUDOTAYIM意味着范围解析运算符(::)实际上在您的代码中PHP无法识别$ class_name范围内的$ static_var,这就是发生错误的原因。
If you still not clear, let me know. Thank you :)
如果你还不清楚,请告诉我。谢谢 :)
#3
1
You really should update your PHP version, 5.2 isn't supported anymore, but ...
你真的应该更新你的PHP版本,不再支持5.2,但是......
... in PHP 5.2 the only way to hack around this is to use eval
:
...在PHP 5.2中,解决这个问题的唯一方法是使用eval:
$return = eval($class_name . '::\\$static_var;');
But be sure to validate $class_name
before you use this, otherwise arbitrary code could be injected (e.g. $class_name = 'do_bad_things(); Class_Name
).
但是在使用它之前一定要验证$ class_name,否则可能会注入任意代码(例如$ class_name ='do_bad_things(); Class_Name)。
#1
1
T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - :: You can try this $class_name = 'Test'; $class_name->$static_var;
T_PAAMAYIM_NEKUDOTAYIM是PHP使用的双冒号范围解析 - ::你可以尝试这个$ class_name ='Test'; $ CLASS_NAME - > $ static_var;
#2
1
@user762799 here is the solution for what you want to do it in php 5.2
@ user762799这里是你想在php 5.2中做到的解决方案
class Sample{
public static $name;
public function __construct(){
self::$name = "User 1";
}
}
$sample = new Sample();
$class = 'Sample';
$name = 'name';
$val_name = "";
$str = '$class::$$name';
eval("\$val_name = \"$str\";");
//echo $val_name."<br>";
eval("\$name = $val_name;");
echo $name;
PAAMAYIM_NEKUDOTAYIM means scope resolution operator(::) actually in your code PHP is unable to identify $static_var
in the scope of $class_name
that is why the error occured.
PAAMAYIM_NEKUDOTAYIM意味着范围解析运算符(::)实际上在您的代码中PHP无法识别$ class_name范围内的$ static_var,这就是发生错误的原因。
If you still not clear, let me know. Thank you :)
如果你还不清楚,请告诉我。谢谢 :)
#3
1
You really should update your PHP version, 5.2 isn't supported anymore, but ...
你真的应该更新你的PHP版本,不再支持5.2,但是......
... in PHP 5.2 the only way to hack around this is to use eval
:
...在PHP 5.2中,解决这个问题的唯一方法是使用eval:
$return = eval($class_name . '::\\$static_var;');
But be sure to validate $class_name
before you use this, otherwise arbitrary code could be injected (e.g. $class_name = 'do_bad_things(); Class_Name
).
但是在使用它之前一定要验证$ class_name,否则可能会注入任意代码(例如$ class_name ='do_bad_things(); Class_Name)。