dqlm80253 2016-12-18 13:44
浏览 42

在创建表单时调用null上的成员函数get()

I want to call method inside class that creates form. But I get this error.

Error: Call to a member function get() on null

This is class that should handle form operations.

class CommentController extends Controller 
{
    public function replyAction()
    {
        $comment = new Comments();

        $form = $this->createFormBuilder($comment)
        ->add('name', TextType::class)
        ->add('text', TextType::class)
        ->add('reply', SubmitType::class, array(
            'label' => 'Reply'))
        ->getForm();

        return $this->$form;
    }
}

and this is how i Call the method from another class

$form = (new CommentController())->replyAction();

It works if I place the replyAction code into class that I am calling it from, but I want to have it inside separate class.

  • 写回答

1条回答 默认 最新

  • doushitang4276 2016-12-18 17:53
    关注

    To answer your actual question, the reason you are getting the error is because something called the container is not being injected into your controller. In theory you could fix this with something like:

    // SomeOtherClass
    $commentController = new CommentController();
    $commentController->setContainer($container);
    $form = $commentController->replyAction(); // Assuming replyAction has 'return $form;'
    

    Needless to say, your SomeOtherClass may or may not have access to the container.

    The real question of course is why you are trying to do such a thing? Because quite frankly, it does not make much sense. It is not uncommon for developers to mix up controllers with services and what not.

    Consider updating your question with an explanation of what you trying to do. Create a form via a service perhaps?

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来