How declare my service via dependency injection in construct? i have a class 


  Paybox


This class is declare as a service.

In my controller class, i inject this service like this :

public function __construct($payboxService) {
    $this-&gt;paybox = $payboxService;
}

public static function create(ContainerInterface $container) {
    return new static(
        $container-&gt;get('paybox_services'),
    );
}


This declaration work fine, but i see sometime this in the constructor :

public function __construct(Paybox $payboxService) {
    $this-&gt;paybox = $payboxService;
}

public static function create(ContainerInterface $container) {
    return new static(
        $container-&gt;get('paybox_services'),
    );
}


They add the Type Paybox before $payboxService.

The 2 method work.

Can someone explain me the difference ? and what is the right way for inject the service ?
Both code snippets work indeed, the second one uses type declaration:

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

You can use type declaration to force specific arguments in your methods parameters. If, for instance, someone should create a new class from your first code snippet and inject something other that your Paybox class your class will not throw errors. In the second example you are enforcing the first parameter to be of type Paybox. If something else is passed PHP will throw errors. If you make your code available as an API for other people they can see what kind of parameters are required without checking the full logic of your encapsulated code.

Furthermore, your IDE will play nice with type hinted parameters providing you with suggestions and better doc block generation.