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
@@ -135,26 +137,26 @@ Container for our application might look like this:
135
137
```php
136
138
class Container
137
139
{
138
-
private $logger;
139
-
private $mailer;
140
+
private ?Logger $logger;
141
+
private ?Mailer $mailer;
140
142
141
-
function getLogger()
143
+
public function getLogger(): Logger
142
144
{
143
-
if (!$this->logger) {
145
+
if (!isset($this->logger)) {
144
146
$this->logger = new FileLogger('log.txt');
145
147
}
146
148
return $this->logger;
147
149
}
148
150
149
-
function getMailer()
151
+
public function getMailer(): Mailer
150
152
{
151
-
if (!$this->mailer) {
153
+
if (!isset($this->mailer)) {
152
154
$this->mailer = new SendMailMailer;
153
155
}
154
156
return $this->mailer;
155
157
}
156
158
157
-
function createNewsletterManager()
159
+
public function createNewsletterManager(): NewsletterManager
158
160
{
159
161
return new NewsletterManager($this->getMailer(), $this->getLogger());
160
162
}
@@ -213,7 +215,7 @@ The container will be generated only once and the code is stored in cache (in di
213
215
During development it is useful to activate auto-refresh mode which automatically regenerate the container when any class or configuration file is changed. Just in the constructor `ContainerLoader` append `true` as the second argument:
214
216
215
217
```php
216
-
$loader = new Nette\DI\ContainerLoader(__DIR__ . '/temp', true);
218
+
$loader = new Nette\DI\ContainerLoader(__DIR__ . '/temp', autoRebuild: true);
217
219
```
218
220
219
221
@@ -244,17 +246,17 @@ Class of the service:
244
246
```php
245
247
class NewsletterManager
246
248
{
247
-
private $anotherService;
249
+
private AnotherService $anotherService;
248
250
249
-
public function setAnotherService(AnotherService $service)
251
+
public function setAnotherService(AnotherService $service): void
250
252
{
251
253
$this->anotherService = $service;
252
254
}
253
255
254
256
...
255
257
```
256
258
257
-
We can also add the `inject: yes` directive. This directive will enable automatic call of `inject*` methods and passing dependencies to public variables with `@inject` annotations:
259
+
We can also add the `inject: yes` directive. This directive will enable automatic call of `inject*` methods and passing dependencies to public variables with #[Inject] attribute:
258
260
259
261
```neon
260
262
services:
@@ -266,21 +268,23 @@ services:
266
268
Dependency `Service1` will be passed by calling the `inject*` method, dependency `Service2` will be assigned to the `$service2` variable:
267
269
268
270
```php
271
+
use Nette\DI\Attributes\Inject;
272
+
269
273
class FooClass
270
274
{
271
-
private $service1;
275
+
private Service1 $service1;
272
276
273
277
// 1) inject* method:
274
278
275
-
public function injectService1(Service1 $service)
279
+
public function injectService1(Service1 $service): void
276
280
{
277
281
$this->service1 = $service1;
278
282
}
279
283
280
-
// 2) Assign to the variable with the @inject annotation:
284
+
// 2) Assign to the variable with the #[Inject] attribute:
281
285
282
-
/** @inject @var Service2 */
283
-
public $service2;
286
+
#[Inject]
287
+
public Service2 $service2;
284
288
}
285
289
```
286
290
@@ -291,17 +295,14 @@ However, this method is not ideal, because the variable must be declared as publ
291
295
Factories
292
296
---------
293
297
294
-
We can use factories generated from an interface. The interface must declare the returning type in the `@return` annotation of the method. Nette will generate a proper implementation of the interface.
298
+
We can use factories generated from an interface. The interface must declare the returning type of the method. Nette will generate a proper implementation of the interface.
295
299
296
300
The interface must have exactly one method named `create`. Our factory interface could be declared in the following way:
297
301
298
302
```php
299
-
interface IBarFactory
303
+
interface BarFactory
300
304
{
301
-
/**
302
-
* @return Bar
303
-
*/
304
-
public function create();
305
+
function create(): Bar;
305
306
}
306
307
```
307
308
@@ -310,7 +311,7 @@ The `create` method will instantiate an `Bar` with the following definition:
310
311
```php
311
312
class Bar
312
313
{
313
-
private $logger;
314
+
private Logger $logger;
314
315
315
316
public function __construct(Logger $logger)
316
317
{
@@ -323,15 +324,15 @@ The factory will be registered in the `config.neon` file:
323
324
324
325
```neon
325
326
services:
326
-
- IBarFactory
327
+
- BarFactory
327
328
```
328
329
329
330
Nette will check if the declared service is an interface. If yes, it will also generate the corresponding implementation of the factory. The definition can be also written in a more verbose form:
330
331
331
332
```neon
332
333
services:
333
334
barFactory:
334
-
implement: IBarFactory
335
+
implement: BarFactory
335
336
```
336
337
337
338
This full definition allows us to declare additional configuration of the object using the `arguments` and `setup` sections, similarly as for all other services.
@@ -341,14 +342,14 @@ In our code, we only have to obtain the factory instance and call the `create` m
0 commit comments