开发者

Syntax error in PHP 5.2

In the php 5.3 I can use class na开发者_StackOverflowme as variable and I can call static variable.

$class_name = 'Test';
$class_name::$static_var;

How to call it in the php 5.2 version?

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ...


T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - :: You can try this $class_name = 'Test'; $class_name->$static_var;


@user762799 here is the solution for what you want to do it in 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.

If you still not clear, let me know. Thank you :)


You really should update your PHP version, 5.2 isn't supported anymore, but ...

... in PHP 5.2 the only way to hack around this is to use 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜