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
Note that the container only creates instances once. It does not work as a factory. You should consider the Factory Pattern instead:
67
+
Note that the container only creates instances once. It does not work as a factory.
68
+
You should consider the [Factory Pattern](https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html) or use the ```create()``` method instead:
67
69
68
70
```php
69
71
use Semperton\Container\Container;
70
72
71
73
class Mail
72
74
{
75
+
public function __construct(Config $c, string $to)
The ```create()``` method will automatically resolve the ```Config``` dependency for ```Mail```.
94
93
95
94
## Configuration
96
95
97
-
You can configure the container with definitions. Closures are always treated as factories and can (!should) be used to bootstrap class instances:
96
+
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:
98
97
99
98
```php
100
99
use Semperton\Container\Container;
101
100
102
101
$container = new Container([
103
102
104
103
'mail' => 'local@host.local',
105
-
'closure' => function () {
104
+
'closure' => function () { // closures must be wrapped in another closure
106
105
return function () {
107
106
return 42;
108
107
};
@@ -112,7 +111,10 @@ $container = new Container([
112
111
113
112
$sender = $c->get('mail');
114
113
return new MailFactory($sender);
115
-
}
114
+
}, // or
115
+
// factory params are automatically resolved from the container
116
+
MailFactory::class => fn (string $mail) => new MailFactory($mail),
117
+
Service::class => fn(Dependency $dep) => new Service($dep)
0 commit comments