M
M
Mikhail Shatilov2016-09-10 16:37:49
symfony
Mikhail Shatilov, 2016-09-10 16:37:49

How to use interface in DI Symfony 3?

I'm trying to pass a class to the constructor. All settings are set.
This is how it works (ShipRepository implements IShipRepository ):

public function __construct(\AppBundle\Infrastructure\Persistence\ShipRepository $shipRepo)

This is not how it works (IShipRepository is an interface):
public function __construct(IShipRepository $shipRepo)

Why can't you specify an interface for a class requirement?
Error code:
Type error: Argument 1 passed to AppBundle\Newsletter\NewsletterManager::__construct() must be an instance of AppBundle\Domain\Repositories\IShipRepository, instance of AppBundle\Infrastructure\Persistence\ShipRepository given, called in \var\cache\dev\appDevDebugProjectContainer.php on line 338
500 Internal Server Error - FatalThrowableError

Classes:
mostly the same as in the ddd example .
UPD: It is not clear why the system requires the interface to be passed specifically, but does not accept a class that implements the desired interface.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Skobkin, 2016-09-10
@skobkin

Can. You are doing something wrong. The class and interface code is not shown, the error is not shown, the container config is not shown - what are we talking about?

D
Dmitry Balobanov, 2016-09-13
@pchelkin

public function __construct(IShipRepository $shipRepo)
To use such an entry, you must first do a use.

M
Maxim, 2016-09-15
@sggr

Apparently AppBundle\Infrastructure\Persistence\ShipRepository does not implement IShipRepository .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question