I'm new to using static methods & properties in classes. What I'm trying to do is run a variable function, but can't use:
self::$static_var()
PHP throws a notice:
Undefined variable: static_var
I have to first assign to a local variable like so:
$local_var = self::$static_var;
Then I can do
$local_var();
Here's some example code. I don't understand why Test 1 doesn't work. I have to do Test 2 in order to get the desired functionality. Question: Why is it that Test 1 doesn't work?
Test 1 - doesn't work
X::do_stuff('whatever');
class X {
public static $static_var = 'print_r';
public static function do_stuff($passed_var) {
self::$static_var($passed_var);
}
}
Test 2 - works
X::do_stuff('whatever');
class X {
public static $static_var = 'print_r';
public static function do_stuff($passed_var) {
$local_var = self::$static_var;
$local_var($passed_var);
}
}
call_user_func(self::$static_var, $passed_var)