0

I have a class's name and I want to call a static method on this class without instantiate it because of the logic in the constructor and the constructor arguments.

Here some basic code of what I want :

class MyClass {

    // Complex constructor
    public function __construct(string $var1, int $var2) {
        // Side effects here
    }

    public static function myStatic(): string {
        return '';
    }

}

// Then I have a function later
function myFunction(string $class) {
    $res = $class::myStatic()    // <-- The goal (obviously don't work since $class is a string...)
}
  • Did you try your code which "obviously doesn't work"? Because it does work. E.g. `$x = 'DateTime'; $d = $x::createFromFormat("Y-m-d", "2022-12-18"); var_dump($d);` – miken32 Apr 12 '22 at 15:16

0 Answers0