Hi, I'm coming from a Java/Spring background. There was a super useful feature in Spring that would let you autowire/inject all implementing classes of an interface like this https://www.baeldung.com/spring-injecting-collections#reference
I was hoping to find some way in PHP-DI to do the same thing. Was planning on using this in an AMQP Message Router. Have some MessageHandler interface which has a canHandle(string)
method and the message router would basically loop through all of those and see if a certain message type could be handled. I was hoping to avoid having to autowire
each individual implementation. Any idea on how I could do something like this, or am I just trying to do something that really isn't in the wheelhouse of PHP-DI?
@autohausen You could use a LoggerFactory for this specific class.
This would require me to use the param-name instead of the interface-name.
Example:
MyClass::class => \DI\autowire()
->constructorParameter('log', MyLogger::class)
I don't like that solution because I have to use class internals within my di-config. Something like that would be much better:
MyClass::class => \DI\autowire()
->override(LoggerInterface::class, MyLogger::class)
I checked the docs but it seems php-di does not support that approach. Anyways. I will stick with the factory approach :-/
Since I try to solve my issue with the factory approach, there is another question: is there a way to get the "target class" within the factory? There is a \DI\Factory\RequestedEntry but this just provides the name of the requested interface (i.e. LoggerInterface). I'd like to somehow get the name of the class requesting that dependency (in my case this would be MyClass::class).
Is that possible somehow?
@odan Thanks for your reply. Injecting a LoggerFactory into my "business" classes moves the configuration and the complexity to the wrong place. My classes just depend on \Psr\Log\LoggerInterface. Anything else is bad design imho.
I decided to stick with this solution:
Convention: always call the ctor-param $log (yikes)
Set the custom logger by explicitly defining the ctor-param:
SomeService::class => \DI\autowire()
->constructorParameter('log', \DI\factory(function(LoggerFactory $loggerFactory){
return $loggerFactory->create('/var/log/my.log');
})),
I don't like it that much, but ey - it works.
Hello guys!
I did not get the idea with @Inject annotations for methods.
https://php-di.org/doc/annotations.html
For example, from doc:
/**
* Annotation combined with the type-hint:
*
* @Inject
*/
public function method1(Foo $param)
{
}
I hope I can call $this->method1() and the object of Foo class will be created and injected as parameter authomatically.
But, obviously, I get "too few arguments Error" while calling.
Please help me understand the idea: how to inject objects as method's argument.
<?php
include 'config.php';
if(isset($_POST["Submit"])){
$tasknamevar = $_POST['taskname'];
//echo($tasknamevar);
INSERT INTO tasks (taskname)
VALUES ('Cardinal');
echo($tasknamevar);
}
?>
<form method="post">
<input type="text" name="taskname" required>
<input type="submit" value="Submit" name="submit">
</form>