Skip to content

Commit ab0a729

Browse files
committed
di/passing-dependencies: better class & variables naming
1 parent 6768aa0 commit ab0a729

17 files changed

+357
-357
lines changed

dependency-injection/bg/passing-dependencies.texy

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
Зависимостите се предават като аргументи на конструктора при създаването на обекта:
2222

2323
```php
24-
class MyService
24+
class MyClass
2525
{
2626
private Cache $cache;
2727

28-
public function __construct(Cache $service)
28+
public function __construct(Cache $cache)
2929
{
30-
$this->cache = $service;
30+
$this->cache = $cache;
3131
}
3232
}
3333

34-
$service = new MyService($cache);
34+
$obj = new MyClass($cache);
3535
```
3636

3737
Тази форма е полезна за задължителни зависимости, които са абсолютно необходими за функционирането на класа, тъй като без тях не може да се създаде инстанция.
@@ -40,10 +40,10 @@ $service = new MyService($cache);
4040

4141
```php
4242
// PHP 8.0
43-
class MyService
43+
class MyClass
4444
{
4545
public function __construct(
46-
private Cache $service,
46+
private Cache $cache,
4747
) {
4848
}
4949
}
@@ -53,10 +53,10 @@ class MyService
5353

5454
```php
5555
// PHP 8.1
56-
class MyService
56+
class MyClass
5757
{
5858
public function __construct(
59-
private readonly Cache $service,
59+
private readonly Cache $cache,
6060
) {
6161
}
6262
}
@@ -71,35 +71,35 @@ class MyService
7171
Зависимостите се предават чрез извикване на метод, който ги съхранява в частно свойство. Обичайната конвенция за именуване на тези методи е `set*()`, затова те се наричат setters.
7272

7373
```php
74-
class MyService
74+
class MyClass
7575
{
7676
private Cache $cache;
7777

78-
public function setCache(Cache $service): void
78+
public function setCache(Cache $cache): void
7979
{
80-
$this->cache = $service;
80+
$this->cache = $cache;
8181
}
8282
}
8383

84-
$service = new MyService;
85-
$service->setCache($cache);
84+
$obj = new MyClass;
85+
$obj->setCache($cache);
8686
```
8787

8888
Този метод е полезен за незадължителни зависимости, които не са необходими за функционирането на класа, тъй като не е гарантирано, че обектът действително ще ги получи (т.е. че потребителят ще извика метода).
8989

9090
В същото време този метод позволява многократно извикване на setter за промяна на зависимостта. Ако това не е желателно, добавете проверка към метода или, от версия PHP 8.1, маркирайте свойството `$cache` с флага `readonly`.
9191

9292
```php
93-
class MyService
93+
class MyClass
9494
{
9595
private Cache $cache;
9696

97-
public function setCache(Cache $service): void
97+
public function setCache(Cache $cache): void
9898
{
9999
if ($this->cache) {
100100
throw new RuntimeException('The dependency has already been set');
101101
}
102-
$this->cache = $service;
102+
$this->cache = $cache;
103103
}
104104
}
105105
```
@@ -109,7 +109,7 @@ class MyService
109109
```neon
110110
services:
111111
-
112-
create: MyService
112+
create: MyClass
113113
setup:
114114
- setCache
115115
```
@@ -121,13 +121,13 @@ services:
121121
Зависимостите се предават директно на свойството:
122122

123123
```php
124-
class MyService
124+
class MyClass
125125
{
126126
public Cache $cache;
127127
}
128128

129-
$service = new MyService;
130-
$service->cache = $cache;
129+
$obj = new MyClass;
130+
$obj->cache = $cache;
131131
```
132132

133133
Този метод се счита за неприемлив, тъй като свойството трябва да бъде декларирано като `public`. Следователно нямаме контрол върху това дали предадената зависимост действително има зададения тип (това беше вярно преди PHP 7.4) и губим възможността да реагираме на новоназначената зависимост със собствен код, например за да предотвратим последващи промени. В същото време свойството става част от публичния интерфейс на класа, което може да е нежелателно.
@@ -137,7 +137,7 @@ $service->cache = $cache;
137137
```neon
138138
services:
139139
-
140-
create: MyService
140+
create: MyClass
141141
setup:
142142
- $cache = @\Cache
143143
```

dependency-injection/cs/passing-dependencies.texy

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ Předávání konstruktorem
2121
Závislosti jsou předávány v okamžiku vytváření objektu jako argumenty konstruktoru:
2222

2323
```php
24-
class MyService
24+
class MyClass
2525
{
2626
private Cache $cache;
2727

28-
public function __construct(Cache $service)
28+
public function __construct(Cache $cache)
2929
{
30-
$this->cache = $service;
30+
$this->cache = $cache;
3131
}
3232
}
3333

34-
$service = new MyService($cache);
34+
$obj = new MyClass($cache);
3535
```
3636

3737
Tato forma je vhodná pro povinné závislosti, které třída nezbytně potřebuje ke své funkci, neboť bez nich nepůjde instanci vytvořit.
@@ -40,10 +40,10 @@ Od PHP 8.0 můžeme použít kratší formu zápisu ([constructor property promo
4040

4141
```php
4242
// PHP 8.0
43-
class MyService
43+
class MyClass
4444
{
4545
public function __construct(
46-
private Cache $service,
46+
private Cache $cache,
4747
) {
4848
}
4949
}
@@ -53,10 +53,10 @@ Od PHP 8.1 lze proměnnou označit příznakem `readonly`, který deklaruje, že
5353

5454
```php
5555
// PHP 8.1
56-
class MyService
56+
class MyClass
5757
{
5858
public function __construct(
59-
private readonly Cache $service,
59+
private readonly Cache $cache,
6060
) {
6161
}
6262
}
@@ -71,35 +71,35 @@ Předávání setterem
7171
Závislosti jsou předávány voláním metody, která je uloží do privátní proměnné. Obvyklou konvencí pojmenování těchto metod je tvar `set*()`, proto se jim říká settery.
7272

7373
```php
74-
class MyService
74+
class MyClass
7575
{
7676
private Cache $cache;
7777

78-
public function setCache(Cache $service): void
78+
public function setCache(Cache $cache): void
7979
{
80-
$this->cache = $service;
80+
$this->cache = $cache;
8181
}
8282
}
8383

84-
$service = new MyService;
85-
$service->setCache($cache);
84+
$obj = new MyClass;
85+
$obj->setCache($cache);
8686
```
8787

8888
Tento způsob je vhodný pro nepovinné závislosti, které nejsou pro funkci třídy nezbytné, neboť není garantováno, že objekt závislost skutečně dostane (tj. že uživatel metodu zavolá).
8989

9090
Zároveň tento způsob připouští volat setter opakovaně a závislost tak měnit. Pokud to není žádoucí, přidáme do metody kontrolu, nebo od PHP 8.1 označíme property `$cache` příznakem `readonly`.
9191

9292
```php
93-
class MyService
93+
class MyClass
9494
{
9595
private Cache $cache;
9696

97-
public function setCache(Cache $service): void
97+
public function setCache(Cache $cache): void
9898
{
9999
if ($this->cache) {
100100
throw new RuntimeException('The dependency has already been set');
101101
}
102-
$this->cache = $service;
102+
$this->cache = $cache;
103103
}
104104
}
105105
```
@@ -109,7 +109,7 @@ Volání setteru definujeme v konfiguraci DI kontejneru v [klíči setup |servic
109109
```neon
110110
services:
111111
-
112-
create: MyService
112+
create: MyClass
113113
setup:
114114
- setCache
115115
```
@@ -121,13 +121,13 @@ Nastavením proměnné
121121
Závislosti jsou předávány zapsáním přímo do členské proměnné:
122122

123123
```php
124-
class MyService
124+
class MyClass
125125
{
126126
public Cache $cache;
127127
}
128128

129-
$service = new MyService;
130-
$service->cache = $cache;
129+
$obj = new MyClass;
130+
$obj->cache = $cache;
131131
```
132132

133133
Tento způsob se považuje za nevhodný, protože členská proměnná musí být deklarována jako `public`. A tudíž nemáme kontrolu nad tím, že předaná závislost bude skutečně daného typu (platilo před PHP 7.4) a přicházíme o možnost reagovat na nově přiřazenou závislost vlastním kódem, například zabránit následné změně. Zároveň se proměnná stává součástí veřejného rozhraní třídy, což nemusí být žádoucí.
@@ -137,7 +137,7 @@ Nastavení proměnné definujeme v konfiraci DI kontejneru v [sekci setup |servi
137137
```neon
138138
services:
139139
-
140-
create: MyService
140+
create: MyClass
141141
setup:
142142
- $cache = @\Cache
143143
```

dependency-injection/de/passing-dependencies.texy

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ Konstruktor-Injektion .[#toc-constructor-injection]
2121
Abhängigkeiten werden als Argumente an den Konstruktor übergeben, wenn das Objekt erstellt wird:
2222

2323
```php
24-
class MyService
24+
class MyClass
2525
{
2626
private Cache $cache;
2727

28-
public function __construct(Cache $service)
28+
public function __construct(Cache $cache)
2929
{
30-
$this->cache = $service;
30+
$this->cache = $cache;
3131
}
3232
}
3333

34-
$service = new MyService($cache);
34+
$obj = new MyClass($cache);
3535
```
3636

3737
Diese Form ist nützlich für obligatorische Abhängigkeiten, die die Klasse unbedingt benötigt, um zu funktionieren, da ohne sie die Instanz nicht erstellt werden kann.
@@ -40,10 +40,10 @@ Seit PHP 8.0 können wir eine kürzere Form der Notation verwenden ([constructor
4040

4141
```php
4242
// PHP 8.0
43-
class MyService
43+
class MyClass
4444
{
4545
public function __construct(
46-
private Cache $service,
46+
private Cache $cache,
4747
) {
4848
}
4949
}
@@ -53,10 +53,10 @@ Seit PHP 8.1 kann eine Eigenschaft mit einem Flag `readonly` markiert werden, da
5353

5454
```php
5555
// PHP 8.1
56-
class MyService
56+
class MyClass
5757
{
5858
public function __construct(
59-
private readonly Cache $service,
59+
private readonly Cache $cache,
6060
) {
6161
}
6262
}
@@ -71,35 +71,35 @@ Setter-Injektion .[#toc-setter-injection]
7171
Abhängigkeiten werden durch den Aufruf einer Methode übergeben, die sie in einer privaten Eigenschaft speichert. Die übliche Namenskonvention für diese Methoden ist die Form `set*()`, weshalb sie auch Setter genannt werden.
7272

7373
```php
74-
class MyService
74+
class MyClass
7575
{
7676
private Cache $cache;
7777

78-
public function setCache(Cache $service): void
78+
public function setCache(Cache $cache): void
7979
{
80-
$this->cache = $service;
80+
$this->cache = $cache;
8181
}
8282
}
8383

84-
$service = new MyService;
85-
$service->setCache($cache);
84+
$obj = new MyClass;
85+
$obj->setCache($cache);
8686
```
8787

8888
Diese Methode ist nützlich für optionale Abhängigkeiten, die für die Funktion der Klasse nicht notwendig sind, da nicht garantiert ist, dass das Objekt sie tatsächlich erhält (d. h. dass der Benutzer die Methode aufruft).
8989

9090
Gleichzeitig ermöglicht diese Methode, dass der Setter wiederholt aufgerufen werden kann, um die Abhängigkeit zu ändern. Wenn dies nicht erwünscht ist, fügen Sie der Methode ein Häkchen hinzu, oder markieren Sie ab PHP 8.1 die Eigenschaft `$cache` mit dem Flag `readonly`.
9191

9292
```php
93-
class MyService
93+
class MyClass
9494
{
9595
private Cache $cache;
9696

97-
public function setCache(Cache $service): void
97+
public function setCache(Cache $cache): void
9898
{
9999
if ($this->cache) {
100100
throw new RuntimeException('The dependency has already been set');
101101
}
102-
$this->cache = $service;
102+
$this->cache = $cache;
103103
}
104104
}
105105
```
@@ -109,7 +109,7 @@ Der Setter-Aufruf wird in der DI-Container-Konfiguration im [Abschnitt setup |se
109109
```neon
110110
services:
111111
-
112-
create: MyService
112+
create: MyClass
113113
setup:
114114
- setCache
115115
```
@@ -121,13 +121,13 @@ Property Injection .[#toc-property-injection]
121121
Abhängigkeiten werden direkt an die Eigenschaft übergeben:
122122

123123
```php
124-
class MyService
124+
class MyClass
125125
{
126126
public Cache $cache;
127127
}
128128

129-
$service = new MyService;
130-
$service->cache = $cache;
129+
$obj = new MyClass;
130+
$obj->cache = $cache;
131131
```
132132

133133
Diese Methode wird als ungeeignet angesehen, da die Eigenschaft als `public` deklariert werden muss. Daher haben wir keine Kontrolle darüber, ob die übergebene Abhängigkeit tatsächlich vom angegebenen Typ ist (dies war vor PHP 7.4 der Fall), und wir verlieren die Möglichkeit, auf die neu zugewiesene Abhängigkeit mit unserem eigenen Code zu reagieren, um zum Beispiel nachträgliche Änderungen zu verhindern. Gleichzeitig wird die Eigenschaft Teil der öffentlichen Schnittstelle der Klasse, was möglicherweise nicht wünschenswert ist.
@@ -137,7 +137,7 @@ Die Einstellung der Variablen wird in der Konfiguration des DI-Containers im [Ab
137137
```neon
138138
services:
139139
-
140-
create: MyService
140+
create: MyClass
141141
setup:
142142
- $cache = @\Cache
143143
```

0 commit comments

Comments
 (0)