Use a variabel as a class name in php or Cakephp 3

Solution:

You need to separate your classname and use variables variable

foreach ($classes as $class) {
    $_class = "\\App\\Command\\{$class}Command";
    $class_name = $class . 'Command';
    $$class_name = new $_class;
    # code...
}

Answer

Solution:

You can do it like this:

foreach ($classes as $class) {
    $classWithNamespace = "\\App\\Command\\{$class}Command";
    $otherCommand = new $classWithNamespace();
}

PHP Sandbox

Source