another trick ala functional programming is returning a closure insidebof a
closure:
array(
'factories' => array(
'db' => function ($sm) {
return function ($constructorArgs) {
return new Db($constructorArgs);
};
},
)
)
you would then use this as
$db = $this->getServiceLocator()->get('db');
$db('someparam');
or
$db->__invoke('someparam');
since its a bit hacky. i am not sure how others would approve it.
On Jan 10, 2014 7:57 PM, "Philip G" <[email protected]> wrote:
> As pointed out earlier by our lord and savior Matthew O'Phinney, Zend\Di is
> becoming less desired component in favor of ServiceManager.
>
> Works for me. I agree SM is much easier to use, except in one instance. So
> I turn to the community to help me resolve this one instance:
>
> > How do I get a ServiceManager object with __constructor() parameters?
>
> My dilemma: I'm building a super-small application with very limited
> requirements that is dependent on PDO.
>
> However, PDO requires constructor parameters. How do I create a PDO loader
> that can 1) be cached by PHP5.5 opcode 2) doesn't require a bunch of other
> classes and 3) can be loaded only at the time I need to make a PDO call.
> (speed of application is #1 requirement)
>
> $sm->get('PDO'); doesn't work since I can't pass in construction
> parameters.
> $sm->get('Db'); won't work either, since construction parameters exist
> within the same file the closure would be included:
>
> ```
> app.config.php = [
> 'sm' => array(
> "factories" => array(
> 'Db' => function ($sm) {
> (PS: Are these cached now? I recall some caching issues
> with embedded closures)
> // $sm->get('Config'); is invalid; not using
> Zend\Application that would inject this.
> //return new PDO(); // No way to get construct parameters
> below...
> },
> )
> ),
> // ....
> "db" => [
> "dsn" =>
>
> 'cassandra:host=127.0.0.1;port=9160,host=127.0.0.2;port=9162;version=3.0.0;dbname=test',
> "username" => null,
> "password" => null,
> ],
> ];
> ```
>
> For Di, I could pass in the parameters: $di->get('PDO', $cfg['db']);
> This isn't possible for SM. This little functionality would make a world of
> difference.
>
> ---
> Philip
> [email protected]
> http://www.gpcentre.net/
>