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?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-10-22 00:14:29
Original
1070 people have browsed it

$new  = new controllers\Order\NewOrder();
$new->setForm(new OrderFormData());
$new->sayForm();
Copy after login
Copy after login
private $form;
public function setForm(\OrderFormData $example){
   $this->form = $example;
}
public function sayForm(){
    echo $this->form->say();
}
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:

$new  = new controllers\Order\NewOrder();
$new->setForm(new OrderFormData());
$new->sayForm();
Copy after login
Copy after login
private $form;
public function setForm(\OrderFormData $example){
   $this->form = $example;
}
public function sayForm(){
    echo $this->form->say();
}
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:
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
Latest Issues
Vim problem under Ubuntu
From 1970-01-01 08:00:00
0
0
0
vim configuration
From 1970-01-01 08:00:00
0
0
0
vim file refresh
From 1970-01-01 08:00:00
0
0
0
mac vim youcomplete prompt - mac vim
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template