You can use contracts i think that's what you're looking for.. See how the laravel 5 default User model class is made. It's using contracts.
as far as i know, contracts are used to decouple a type of service from its concrete implementation. therefore the implemenation is interchangable. but only one implementation can be bound to a contract at the same time. usage would be to type hint the contract as i do not care which implemenation is bound. what i want to achive is to have only one binding/closure/factory for different concrete services. when i type hint on concrete service that this one binding can handle it should. different purpose than contracts at this time i think. (they could also be extended in that way though..) the more i think of it, i think contracts are the exact opposite of my thing.
no, thats not what i mean either. please take a look at http://framework.zend.com/manual/current/en/modules/zend.service-manager.intro.html head down to "abstract factory"
Ok I see what it does, still I have a hard time figuring out an use case where it could be used. Maybe give us a more concrete example? In what occasion did you use this ZF2 feature ?
i have several use cases. one is as above mentioned doctrine repositories. if you have ten entities you would have to define ten bindings to the container. if i could define one binding for every class that implements the repostory interface i would save 9 bindings. similar applies to several api synchronisation services i am running. those are separated into several classes for different purposes, but every class requires the basic webservice client it can use to run the queries. i have to define a binding for each service which are essentially all the same.
implementation in the service container would be rather simple imo.
Ok, I can see the pratical use of it, but I can't figure out how you would let the IoC container know which class do you need without a binding in a dependency injection scenario.
example :
use SomeServiceInterface;
class SomeCommand {
public function __construct(SomeServiceInterface $service)
}
In that case how can your abstract factory know if you want the A or B implementation of SomeServiceInterface ?
your example does not apply to the problem. in fact this would be more appropriate for the contextual binding or contracts.
an example for my idea would be the following:
// only one single binding which can make every repo
$app->bindInterface('Repository', function($app, $requestedClass) {
if (!$requestedClass instanceof Repository) {
return false;
}
$doctrine = $app->make('Doctrine');
return $doctrine->getRepository($requestedClass);
});
class ARepo extends Repository {
}
class BRepo extends Repository {
}
..
class ZRepo extends Repository {
}
// get any repo. Container has to check if the given class implements a bound interface and use that closure/factory
$myBRepo = $app->make(ZRepo::class);
class SomeCommand {
public function __construct(ZRepo $myRepo)
}
I'm not familiar with the ins and outs of doctrine, but I get the idea.
Maybe you should look at http://laravel.com/docs/5.0/container#container-events , I didn't try them yet, so am not sure it will solve this problem.
Otherwise, I think it's a nice concept, and if it's not possible without a core modification, you should submit it on github.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community