Skip to content

Commit 1f682ca

Browse files
committed
-
1 parent f1f43a7 commit 1f682ca

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

service_container.rst

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,26 @@ as arguments of other services:
334334
services:
335335
App\Service\SomeService:
336336
arguments:
337-
# arguments without a type are treated as strings
337+
# string, numeric and boolean arguments can be passed "as is"
338338
- 'Foo'
339-
# explicitly declare a string argument
340-
- !str 'Foo'
339+
- true
340+
- 7
341+
- 3.14
342+
341343
# constants can be built-in, user-defined, or Enums
342344
- !php/const true
343345
- !php/const E_ALL
344346
- !php/const PDO::FETCH_NUM
345347
- !php/const App\Service\AnotherService::SOME_CONSTANT
346348
- !php/const App\Config\SomeEnum::SomeCase
349+
347350
# when not using autowiring, you can pass service arguments explicitly
348351
- '@some-service-id' # the leading '@' tells this is a service ID, not a string
349352
- '@?some-service-id' # using '?' means to pass null if service doesn't exist
353+
354+
# binary contents are passed encoded as base64 strings
355+
- !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
356+
350357
# collections (arrays) can include any type of argument
351358
-
352359
first: !php/const true
@@ -366,20 +373,30 @@ as arguments of other services:
366373
367374
<services>
368375
<service id="App\Service\SomeService">
369-
<!-- arguments without a type are treated as strings -->
376+
<!-- arguments without a type can be strings or numbers -->
370377
<argument>Foo</argument>
378+
<argument>7</argument>
379+
<argument>3.14</argument>
371380
<!-- explicitly declare a string argument -->
372381
<argument type="string">Foo</argument>
382+
<!-- booleans are passed as constants -->
383+
<argument type="constant">true</argument>
384+
373385
<!-- constants can be built-in, user-defined, or Enums -->
374386
<argument type="constant">true</argument>
375387
<argument type="constant">E_ALL</argument>
376388
<argument type="constant">PDO::FETCH_NUM</argument>
377389
<argument type="constant">App\Service\AnotherService::SOME_CONSTANT</argument>
378390
<argument type="constant">App\Config\SomeEnum::SomeCase</argument>
391+
379392
<!-- when not using autowiring, you can pass service arguments explicitly -->
380393
<argument type="service"
381394
id="some-service-id"
382395
on-invalid="dependency_injection-ignore"/>
396+
397+
<!-- binary contents are passed encoded as base64 strings -->
398+
<argument type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</argument>
399+
383400
<!-- collections (arrays) can include any type of argument -->
384401
<argument type="collection">
385402
<argument key="first" type="constant">true</argument>
@@ -402,18 +419,25 @@ as arguments of other services:
402419
$services = $container->services();
403420
404421
$services->set(App\Service\SomeService::class)
405-
// string arguments
422+
// string, numeric and boolean arguments can be passed "as is"
406423
->arg(0, 'Foo')
407-
// constants: built-in, user-defined, or Enums
408424
->arg(1, true)
409-
->arg(2, E_ALL)
410-
->arg(3, \PDO::FETCH_NUM)
411-
->arg(4, \App\Service\AnotherService::SOME_CONSTANT)
412-
->arg(5, \App\Config\SomeEnum::SomeCase)
413-
// explicit service reference with on-invalid behavior
414-
->arg(6, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
425+
->arg(2, 7)
426+
->arg(3, 3.14)
427+
428+
// constants: built-in, user-defined, or Enums
429+
->arg(4, E_ALL)
430+
->arg(5, \PDO::FETCH_NUM)
431+
->arg(6, \App\Service\AnotherService::SOME_CONSTANT)
432+
->arg(7, \App\Config\SomeEnum::SomeCase)
433+
434+
// when not using autowiring, you can pass service arguments explicitly
435+
->arg(8, service('some-service-id')) # fails if service doesn't exist
436+
# passes null if service doesn't exist
437+
->arg(9, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
438+
415439
// collection with mixed argument types
416-
->arg(7, [
440+
->arg(10, [
417441
'first' => true,
418442
'second' => 'Foo',
419443
]);

0 commit comments

Comments
 (0)