1

In the php 5.3 I can use class name 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 ...
2

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

@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 :)

Comments

1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.