PHP DI and casting interface
Clash Royale CLAN TAG#URR8PPP
PHP DI and casting interface
I do not understand, for example I'm using php-di container,
I have ClassA and ClassB, both of them are implements Intreface1
And I'm creating objects of this two classes thru DI
Now when I'm injecting ClassA to the constructor I need inject exactly this class, which crate a dependency of this classA, not an Intreface1
Can I somehow say that this particular ClassA should implement intrefeca1 ?
In docs of PHP-DI I found
// mapping an interface to an implementation
'LoggerInterface' => DIcreate('MyLogger'),
But I do not understand how it works, LoggerIntreace it's just a string which maps this text to to the object
1 Answer
1
You can inject "by interface"
example with monolog and php-di, you say :
use MonologLogger;
use PsrLogLoggerInterface;
...
LoggerInterface::class => DIautowire(Logger::class)
and now in your constructor, you can inject
public function __construct(
PsrLogLoggerInterface $logger
)
$this->logger = $logger;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.