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

WBOY
Release: 2016-10-22 00:14:29
Original
965 people have browsed it

<code class="php">$new  = new controllers\Order\NewOrder();
$new->setForm(new OrderFormData());
$new->sayForm();</code>
Copy after login
Copy after login
<code class="php">private $form;
public function setForm(\OrderFormData $example){
   $this->form = $example;
}
public function sayForm(){
    echo $this->form->say();
}
</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login
<code class="php">private $form;
public function setForm(\OrderFormData $example){
   $this->form = $example;
}
public function sayForm(){
    echo $this->form->say();
}
</code>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!