How can I call a static method from a class if all I have is a string of the class name?

后端 未结 6 1290
自闭症患者
自闭症患者 2020-12-09 01:05

How would I get something like this to work?

$class_name = \'ClassPeer\';
$class_name::doSomething();
6条回答
  •  佛祖请我去吃肉
    2020-12-09 01:56

    After I have almost missed the simplest solution from VolkerK, I have decided to extend and put it in a post. This is how to call the static members on the instance class

    // calling class static method
    $className = get_class($this);
    $result = $className::caluclate($arg1, $arg2);
    
    // using class static member
    foreach ($className::$fields as $field) {
      :
    }
    

提交回复
热议问题