In PHP7.4.3, I'm trying to use class static variable to refer to different class static member functions as below:
1 class ColorT {
2 static $color = "yellow";
3 static function yellow() {
4 echo "yellow"."<br>";
5 }
6 static function green() {
7 echo "green"."<br>";
8 }
9 }
10 ColorT::$color(); //ColorT::yellow() function is expected to be called
11 $global_color = "yellow";
12 ColorT::$global_color(); //ColorT::yellow() function is expected to be called
Both line 10 and line 12, I expect ColorT::yellow() to be invoked.
Line 12 works as expected.
But in line 10, it print error :
PHP Fatal error: Uncaught Error: Function name must be a string
Doesn't php support class static variable referring to class static member functions?
If it's supported, then how to fix the error mentioned in line 10?
ColorT::{ColorT::$color}();help? I'm not sure if your priority is to only use the classname once, or just to be able to call the method dynamically in one line. That should work as far back as PHP 5.4. See 3v4l.org/kbBVq