You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
withEntry(string $id, mixed $entry): Container // add a container entry
31
+
withDelegate(ContainerInterface $delegate): Container // register a delegate container
32
+
get(string $id): mixed // get entry (PSR-11)
31
33
has(string $id): bool // has entry (PSR-11)
32
-
create(string $id, array $params = []); // create a class with optional constructor substitution args
34
+
create(string $id, array $params = []): mixed // create a class with optional constructor substitution args
33
35
entries(): array // list all container entries
34
36
```
35
37
@@ -70,7 +72,7 @@ $hello === $hello2 // true
70
72
$hello->print(); // 'Hello World'
71
73
```
72
74
73
-
Note that the container only creates instances once. It does not work as a factory.
75
+
Note that the container only creates (shared) instances once. It does not work as a factory.
74
76
You should consider the [Factory Pattern](https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html) or use the ```create()``` method instead:
75
77
76
78
```php
@@ -99,7 +101,7 @@ The ```create()``` method will automatically resolve the ```Config``` dependency
99
101
100
102
## Configuration
101
103
102
-
You can configure the container with definitions. ```Callables```(except invokable objects) are always treated as factories and can (!should) be used to bootstrap class instances:
104
+
You can configure the container with definitions. ```Closures``` are always treated as factories and should be used to bootstrap class instances. If you like to use ```callables``` as factories: ```Closure::fromCallable([$object, 'method'])```.
$container->get(MailFactory::class); // instance of MailFactory
129
131
```
130
132
131
-
The ```with()``` method also treats ```callables``` as factories.
133
+
The ```withEntry()``` method also treats ```callables``` as factories.
132
134
133
135
## Immutability
134
136
135
-
Once the container is created, it is immutable. If you like to add an entry after instantiation, keep in mind that the ```with()``` method always returns a new container instance:
137
+
Once the container is created, it is immutable. If you like to add an entry after instantiation, keep in mind that the ```withEntry()``` method always returns a new container instance:
0 commit comments