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 ...
Gowri
  • 16,587
  • 26
  • 100
  • 160
user762799
  • 23
  • 4
  • 1
    What about `{$class_name}::{$static_var}` ? – alex Oct 14 '11 at 07:15
  • you can refer these below links for your task. [http://stackoverflow.com/questions/4995540/unexpected-t-paamayim-nekudotayim-in-php-5-2-x](http://stackoverflow.com/questions/4995540/unexpected-t-paamayim-nekudotayim-in-php-5-2-x) OR [http://stackoverflow.com/questions/3679717/unexpected-t-paamayim-nekudotayim-on-one-computer-but-not-another-with-php-5](http://stackoverflow.com/questions/3679717/unexpected-t-paamayim-nekudotayim-on-one-computer-but-not-another-with-php-5) This will helpful to you.. Thanks. – Chandresh M Oct 14 '11 at 07:11

3 Answers3

1

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

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

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62
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).

NikiC
  • 100,734
  • 37
  • 191
  • 225