从字符串变量构造类会导致ClassNotFoundException

时间:2018-03-19 16:45:05

标签: php

当我尝试从字符串变量构造新类时,它会抛出ClassNotFoundException。我也尝试在名称之前添加类名称空间,但仍无法正常工作。

public function registerCommands(){
    $commandMap = $this->getServer()->getCommandMap();
    $commands = $this->getCommands();
    foreach($commands as $class => $enterName){
        $commandMap->register("command", new $class($enterName));
    }
}

第二次尝试

public function registerCommands(){
    $commandMap = $this->getServer()->getCommandMap();
    $commands = $this->getCommands();
    foreach($commands as $class => $enterName){
        $class = "\\server\utils\CommandReader\\" . $class;
        $commandMap->register("command", new $class($enterName));
    }
}

$ class是string

0 个答案:

没有答案