<code class="php">$new = new controllers\Order\NewOrder(); $new->setForm(new OrderFormData()); $new->sayForm();</code>
<code class="php">private $form; public function setForm(\OrderFormData $example){ $this->form = $example; } public function sayForm(){ echo $this->form->say(); } </code>
I’ve been learning dependency injection recently, and I don’t quite understand what the value passed in the above code means. (OrderFormData $example)
This is a class value. What does it represent? Can I write it casually? And this Are there any requirements for this writing method? Thanks to all the great gods
<code class="php">$new = new controllers\Order\NewOrder(); $new->setForm(new OrderFormData()); $new->sayForm();</code>
<code class="php">private $form; public function setForm(\OrderFormData $example){ $this->form = $example; } public function sayForm(){ echo $this->form->say(); } </code>
I’ve been learning dependency injection recently, and I don’t quite understand what the value passed in the above code means. (OrderFormData $example)
This is a class value. What does it represent? Can I write it casually? And this Are there any requirements for this writing method? Thanks to all the great gods
This way of writing is called type hint, which is the setForm
method statement. My first parameter must be an instance of OrderFormData
. Otherwise, it will not be passed. After writing type hint, when the reflection class scans the method parameters, it will Automatically help you new OrderFormData