php

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


How would I get something like this to work?

$class_name = 'ClassPeer';
$class_name::doSomething();

Solution

  • $class_name = 'Test';
    $method_name = 'doSomething';
    $params = ['something'];
    $class_name::$method_name(...$params);
    

    Or, for legacy version of PHP:

    call_user_func(array($class_name, 'doSomething'));
    call_user_func($class_name .'::doSomething'); // >5.2.3