Home >Backend Development >PHP Tutorial >mysql - Please tell me about the parameters passed into PHP methods and the applicable scenarios of dependency injection?

mysql - Please tell me about the parameters passed into PHP methods and the applicable scenarios of dependency injection?

WBOY
WBOYOriginal
2016-10-22 00:14:291034browse

<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

Reply content:

<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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn