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:



If you enjoyed this post, please consider supporting this site!