2016-08-30 2 views
0

Ich habe 3 Dateien:Anruf auf eine Elementfunktion addPaiementType() auf null

Die erste:

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

Zweitens:

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(); 
    } 
} 

und die Steuerung:

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

Aber Silex geben Sie mir den Fehler:

Call to a member function addPaiementType() on null 

Ich verstehe nicht warum. Für mich entspricht diese Codestruktur:

$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(); 

Aber es scheint nicht ... Weiß nicht warum.

Vielen Dank für Hilfe

Antwort

2

Um Objektaufruf Verkettungs zu verwenden, müssen die Methoden $this zurückzukehren. Du machst das nicht. Ihre addDuree() hat NOreturn überhaupt, so hat es implizit eine return null, was bedeutet, dass diese Zeile:

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

ausführt, als ob es geschrieben wurde

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

Ihr sollte

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

In der Tat, danke @Marc B – Macbernie

Verwandte Themen