在 null 上调用成员函数 addPaiementType() [英] Call to a member function addPaiementType() on null

查看:28
本文介绍了在 null 上调用成员函数 addPaiementType()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个文件:

第一个:

public function register(\Pimple\Container $app)
{
    $app['manager.form'] = function() use ($app) {
        return new Form($app);
    };
}

第二:

class Form
{
    private $form;

    public function __construct(Application $app)
    {
        $this->form = $app['form.factory']->createBuilder(FormType::class);
    }

    public function addDuree()
    {
        $this->form->add('duree', ChoiceType::class, [
            'choices' => [
                '1'  => '1',
                '3'  => '3',
                '6'  => '6',
                '12' => '12'
            ],
            'multiple' => false,
            'expanded' => true,
            'data' => 1
        ]);
    }

    public function addPaiementType()
    {
        $this->form->add('paiementType', ChoiceType::class, [
            'choices' => [
                'virement'  => 'virement',
                'cheque'    => 'cheque',
                'paypal'    => 'paypal',
                'paypal-cb' => 'paypal-cb'
            ],
            'multiple' => false,
            'expanded' => true,
            'data' => 'virement'
        ]);
    }

    public function addTermsAccepted()
    {
        $this->form->add('termsAccepted', CheckboxType::class, [
            'mapped' => false,
            'constraints' => new Assert\IsTrue(),
        ]);
    }

    public function getForm()
    {
        return $this->form->getForm();
    }
}

和控制器:

$form = $app['manager.form']->addDuree()->addPaiementType()->addTermsAccepted();

但是 Silex 给了我错误:

But Silex give me the error:

Call to a member function addPaiementType() on null

我不明白为什么.对我来说,这个代码结构相当于:

I do not understand why. For me this code structure is equivalent to:

    $form = $app['form.factory']->createBuilder(FormType::class)
    ->add('duree', ChoiceType::class, [
        'choices' => [
            '1'  => '1',
            '3'  => '3',
            '6'  => '6',
            '12' => '12'
        ],
        'multiple' => false,
        'expanded' => true,
        'data' => 1
    ])
    ->add('paiementType', ChoiceType::class, [
        'choices' => [
            'virement'  => 'virement',
            'cheque'    => 'cheque',
            'paypal'    => 'paypal',
            'paypal-cb' => 'paypal-cb'
        ],
        'multiple' => false,
        'expanded' => true,
        'data' => 'virement'
    ])
    ->add('termsAccepted', CheckboxType::class, [
        'mapped' => false,
        'constraints' => new Assert\IsTrue(),
    ])
    ->getForm();

但似乎不是......不知道为什么.

But it seems not... Don't know why.

感谢帮助

推荐答案

要使用对象调用链,方法必须返回 $this.你不这样做.你的 addDuree() 根本没有 NO return,所以它隐式有一个 return null,这意味着这行:

To use object call chaining, the methods have to return $this. You're not doing that. Your addDuree() has NO return at all, so it implicitly has a return null, which means that this line:

$form = $app['manager.form']->addDuree()->addPaiementType()->addTermsAccepted();

像写入一样执行

 $form = $app['manager.form']->null->addPaiementType()
                               ^^^^

你应该有

function addPaimentType() {
    ... stuff ...
    return $this;
}

这篇关于在 null 上调用成员函数 addPaiementType()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆