Skip to content

Commit 3e0ca06

Browse files
committed
forms: used constructor promotion for form data class
1 parent f3220d0 commit 3e0ca06

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

cs/form-presenter.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ class RegistrationFormData
234234
}
235235
```
236236

237+
Od PHP 8.0 můžete použít tento elegantní zápis, který využívá konstruktor:
238+
239+
```php
240+
class RegistrationFormData
241+
{
242+
public function __construct(
243+
public string $name,
244+
public int $age,
245+
public string $password,
246+
) {
247+
}
248+
}
249+
```
250+
237251
Jak říci Nette, aby nám data vracel jako objekty této třídy? Snadněji než si myslíte. Stačí pouze třídu uvést jako typ parametru `$data` v obslužné metodě:
238252

239253
```php

cs/form-standalone.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ class RegistrationFormData
217217
}
218218
```
219219

220+
Od PHP 8.0 můžete použít tento elegantní zápis, který využívá konstruktor:
221+
222+
```php
223+
class RegistrationFormData
224+
{
225+
public function __construct(
226+
public string $name,
227+
public int $age,
228+
public string $password,
229+
) {
230+
}
231+
}
232+
```
233+
220234
Jak říci Nette, aby nám data vracel jako objekty této třídy? Snadněji než si myslíte. Stačí pouze název třídy nebo objekt k hydrataci uvést jako parametr:
221235

222236
```php

en/form-presenter.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ class RegistrationFormData
234234
}
235235
```
236236

237+
As of PHP 8.0, you can use this elegant notation that uses a constructor:
238+
239+
```php
240+
class RegistrationFormData
241+
{
242+
public function __construct(
243+
public string $name,
244+
public int $age,
245+
public string $password,
246+
) {
247+
}
248+
}
249+
```
250+
237251
How to tell Nette to return data as objects of this class? Easier than you think. All you have to do is specify the class as the type of the `$data` parameter in the handler:
238252

239253
```php

en/form-standalone.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ class RegistrationFormData
217217
}
218218
```
219219

220+
As of PHP 8.0, you can use this elegant notation that uses a constructor:
221+
222+
```php
223+
class RegistrationFormData
224+
{
225+
public function __construct(
226+
public string $name,
227+
public int $age,
228+
public string $password,
229+
) {
230+
}
231+
}
232+
```
233+
220234
How to tell Nette to return data to us as objects of this class? Easier than you think. All you have to do is specify the class name or object to hydrate as a parameter:
221235

222236
```php

0 commit comments

Comments
 (0)