Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing static methods using a variable class name (PHP)

I am trying to access a static method, but using a variable as the class name. Is this possible? I seem to be having issues with it. I want to be able to do something like this:

class foo {
    public static function bar() {
        echo 'test';
    }
}

$variable_class_name = 'foo';
$variable_class_name::bar();

And I want to be able to do similar using static variables as well.

like image 345
dqhendricks Avatar asked Feb 20 '11 20:02

dqhendricks


1 Answers

That syntax is only supported in PHP 5.3 and later. Previous versions don't understand that syntax, hence your parse error (T_PAAMAYIM_NEKUDOTAYIM refers to the :: operator).

In previous versions you can try call_user_func(), passing it an array containing the class name and its method name:

$variable_class_name = 'foo';
call_user_func(array($variable_class_name, 'bar'));
like image 130
BoltClock Avatar answered Oct 12 '22 20:10

BoltClock