-
Notifications
You must be signed in to change notification settings - Fork 68
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
Injection of already instantiated object #159
Comments
You'll want to check out the documentation on substitution rules. |
From a purely theoretical point of view, if you're using a DIC, the DIC would normally have the job of creating the instances so the answer would normally be to give dice the rule for constructing the object. However, with that said, there are a few different ways to reference existing objects, depending on what you're trying to do. You can supply an existing instance either as $instance = new MyInstance();
//Configure dice to substitute any type hinted `SomeInterface` with $instance
$dice = $dice->addRule('SomeClass', [
'substitutions' => ['SomeInterface' => [\Dice\Dice::INSTANCE => $instance ] ]
]);
// Or pass it in as a constructor argument
$dice = $dice->addRule('SomeClass', [
'constructParams' => [$instance]
]); You can also set up a global substitution so it gets applied to any type-hinted constructor argument: //Configure dice to substitute any type hinted `SomeInterface` with $instance
$dice = $dice->addRule('*', [
'substitutions' => ['SomeInterface' => [\Dice\Dice::INSTANCE => $instance ] ]
]); If you need more control, Dice also calls closures if they are provided as instances: $dice = $dice->addRule('*', [
'substitutions' => ['SomeInterface' => [\Dice\Dice::INSTANCE => function() use ($instance) {
return $instance
} ]
]
]); Finally, if your existing object is coming from a factory or another DIC you can use call with chaining: $dice = $dice->addRule('Something', [
'instanceOf' => 'Pimple'
'call' => [
['get', ['SomeClass'], \Dice\Dice::CHAIN_CALL ]
]
]):
//Calls $pimple->get('SomeClass') and returns the instance
var_dump($dice->create('Something')) |
That is brilliant. I was pretty sure I was missing something and I did. That works perfectly well. Thank you for explaining. |
Is this all still valid? As I've just tried this as my code is a module for someone else's software, so some objects I need are already instantiated, nothing I can do about that.
Where $db is a valid PDO object from the main application. When I do this, Dice errors with:
Also tried it using closures:
That gives me a PDO exception for invalid DSN - So it looks like it is still trying to construct a new PDO object rather than sub in mine. Thanks. |
@KarlAustin All you need is $di = $di->addRules([
'*' => [
'substitutions' => [
'PDO' => $db
],
]]);
That said, why not allow Dice to create your PDO object on demand and mark it as shared? |
Is there a way to inject already instantiated object so it can be resolved as shared with further
$dice->create()
calls?From my quick glance at the code adding new entry in
$instances
array should do the trick. Unfortunately I cannot find the proper method to do this and$instances
property is private so can't do it even by extending theDice
class.Am I missing something?
The text was updated successfully, but these errors were encountered: