PHP instantiate class string


If you have a variable with a fully qualified class name:

$className = MyClass::class;

You can use the variable to instantiate the class:

$classInstance = new $className();

Demo

Repl.it:



Please support this site and join our Discord!