Skip to content

Commit 5b1acac

Browse files
committed
added info to readme about using parameter order with define()
1 parent 41cf217 commit 5b1acac

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ $myObj = $injector->make('MyClass');
257257
var_dump($myObj instanceof MyClass); // true
258258
```
259259

260+
> **NOTE:** Since this `define()` call is passing raw values (as evidenced by the colon `:` usage),
261+
you can achieve the same result by omitting the array key(s) and relying on parameter order rather
262+
than name. Like so: `$injector->define('MyClass', [$dependencyInstance]);`
263+
260264
###### Specifying Injection Definitions On the Fly
261265

262266
You may also specify injection definitions at call-time with `Auryn\Injector::make`. Consider:
@@ -329,12 +333,12 @@ on type-hints, class name definitions and existing instances. But what happens i
329333
a scalar or other non-object variable into a class? First, let's establish the following behavioral
330334
rule:
331335

332-
> **IMPORTANT:** The Injector assumes all injection definitions are class names by default.
336+
> **IMPORTANT:** The Injector assumes all named-parameter definitions are class names by default.
333337
334-
If you want the Injector to treat a parameter definition as a "raw" value and not a class name, you
335-
must prefix the parameter name in your definition with a colon character `:`. For example, consider
336-
the following code in which we tell the Injector to share a `PDO` database connection instance and
337-
define its scalar constructor parameters:
338+
If you want the Injector to treat a named-parameter definition as a "raw" value and not a class name,
339+
you must prefix the parameter name in your definition with a colon character `:`. For example,
340+
consider the following code in which we tell the Injector to share a `PDO` database connection
341+
instance and define its scalar constructor parameters:
338342

339343
```php
340344
<?php
@@ -356,6 +360,12 @@ easily specified arrays or integers or any other data type in the above definiti
356360
parameter name is prefixed with a `:`, Auryn will inject the value directly without attempting to
357361
instantiate it.
358362

363+
> **NOTE:** As mentioned previously, since this `define()` call is passing raw values, you may opt to
364+
assign the values by parameter order rather than name. Since PDO's first three parameters are `$dsn`,
365+
`$username`, and `$password`, in that order, you could accomplish the same result by leaving out the
366+
array keys, like so:
367+
`$injector->define('PDO', ['mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass']);`
368+
359369
### Global Parameter Definitions
360370

361371
Sometimes applications may reuse the same value everywhere. However, it can be a hassle to manually

0 commit comments

Comments
 (0)