这是一个方法,其中传递一个字符串,使用该字符串创建一个实例。示例方法:
public function action($actionType)
{
//var_dump(new $actionType);
if (!class_exists($actionType)) {
//throw new Exception
}
if (!(new $actionType) instanceof ActionInterface) {
////throw new Exception
}
$actionType = new $actionType;
echo $actionType->doAction();
}
但是我收到了一个找不到类的错误,但是当我手动编写类名或手动追加命名空间$actionType = __namespace__ . "\\$actionType";
时,就会出错。为什么会这样?
答案 0 :(得分:0)
试试这个:
public function action($actionType) {
//var_dump(new $actionType);
if (!class_exists($actionType)) {
//throw new Exception
}
$actionType = new $actionType;
if (!($actionType instanceof ActionInterface)) {
////throw new Exception
}
echo $actionType->doAction();
}