Access a static variable by $var::$reference

前端 未结 4 625
温柔的废话
温柔的废话 2020-12-10 00:30

I am trying to access a static variable within a class by using a variable class name. I\'m aware that in order to access a function within the class, you

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 01:23

    Have you try this?

    class foo {
        static public $bar = "Hi";
    
        static public function bar() {
            echo "Hi";
        }
    }
    
    echo foo::$bar; // Output: Hi
    foo::bar(); // Output: Hi
    
    $class = "foo";
    echo $class::$bar; // Output: Hi
    $class::bar(); // Output: Hi
    call_user_func($class, 'bar'); // Output: Hi
    

提交回复
热议问题