【发布时间】:2015-04-07 18:20:43
【问题描述】:
我需要将不同的对象实例化到同一个方法中。 我在这里找到了解决方案:
Creating PHP class instance with a string
但是当我在 Symfony2 的控制器上使用它时,我遇到了这个错误:
尝试从全局命名空间加载类“PhotoType”。 您是否忘记了“使用”声明?
我不明白,因为我已经添加了我所有的“使用”
namespace DIVE\FileUploaderBundle\Controller;
use DIVE\FileUploaderBundle\Entity\Photo;
use DIVE\FileUploaderBundle\Form\PhotoType;
...
class DefaultController extends Controller {
public function listFileAction($fileType) {
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository("FDMFileUploaderBundle:".$fileType);
$files = $repository->findAll();
$forms = array();
foreach ($files as $file) {
$class = $fileType."Type";
array_push($forms, $this->get('form.factory')->create(new $class(), $file));
}
$formViews = array();
foreach ($forms as $form) {
array_push($formViews, $form->createView());
}
return $this->render("FDMFileUploaderBundle:Default:list".$fileType.".html.twig", array(
"forms" => $formViews
)
);
}
}
对不起我的英语,我正在学习它。
【问题讨论】:
-
如果我不得不猜测我认为它假设 PhotoClass 在全局命名空间中。从字符串调用类名时,尝试将 'DIVE\FileUploaderBundle\Form\' 添加到类名中。
标签: php symfony inheritance