-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dealing with splat operator in a Factory #802
Comments
🤔 that's a good question, honestly I'm not 100% sure. I would personally test passing the |
Yeah I gave it a try already with eyes closed. Sadly it didn't work |
This issue seems related to #619, and I just ran into this problem. In the following code and error messages, the names have been changed to protect the guilty. 😉 I have a class defined something like this: final class AggregateThingService implements ThingServiceProviderInterface
{
public function __construct(
private readonly ThingServiceProviderInterface $defaultProvider,
ThingServiceProviderInterface ...$additionalProviders,
) {
}
} The idea is that it requires a default provider, but you can also give it additional providers. This "aggregate" class implements the same interface, and can be used in the same way as the ones passed to it, but it has some additional logic about which provider to use when executing the interface's methods. For PHP-DI, my definition looks something like this. In this case, I'm not passing any additional providers. I don't need them for this case. I just need the default provider, which I provide. [
ThingServiceProviderInterface::class => DI\create(AggregateThingService::class)
->constructor(
defaultProvider: DI\get(CompanyProvider::class),
),
] When PHP-DI tries to process this, it gives the following error:
|
Would it work with final class AggregateThingService implements ThingServiceProviderInterface
{
public function __construct(
private readonly ThingServiceProviderInterface $defaultProvider,
ThingServiceProviderInterface ...$additionalProviders = null,
) {
}
} ? (i.e. suggesting to PHP-DI that these extra parameters are optional) But maybe I'm just off… Does the splat operator make the parameter optional already? |
Yes it does |
My question would be if it's possible somehow to tell
\DI\FactoryInterface::make
how to handle a variadic constructor.So imagine the following class:
My factory method obviously wont work this way, and now I can only instantiate the Test class by hand. Is there a way currently to handle this case or if not, would it be possible to support it somehow?
The text was updated successfully, but these errors were encountered: