我试图动态地初始化一个表单.表单名称存储在$ obj-> getFormTypeName()中.
$formName = 'Acme\testBundle\Form\CustomType\\'.ucfirst($obj->getFormTypeName()).'Type';
$contactForms[$obj->getId()] = $this->createForm($formName::class);
Run Code Online (Sandbox Code Playgroud)
但我得到错误: Compile Error: Dynamic class names are not allowed in compile-time ::class fetch
有没有办法用symfony中的Scope Resolution Operator动态初始化表单?
谢谢阅读.
为了解决这个问题,我会做类似的事情:
$contactForms[$obj->getId()] = $this->createForm(
'Acme\testBundle\Form\CustomType\' . ucfirst($obj->getFormTypeName()) . 'Type'
);
Run Code Online (Sandbox Code Playgroud)
毕竟,:: class只返回类的完全限定名.