@@ -36,7 +36,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,
36
36
}
37
37
}
38
38
39
- When accessing the ``$options ``, you need to add a lot of boilerplate code to
39
+ When accessing the ``$options ``, you need to add some boilerplate code to
40
40
check which options are set::
41
41
42
42
class Mailer
@@ -46,29 +46,17 @@ check which options are set::
46
46
{
47
47
$mail = ...;
48
48
49
- $mail->setHost(isset($this->options['host'])
50
- ? $this->options['host']
51
- : 'smtp.example.org');
52
-
53
- $mail->setUsername(isset($this->options['username'])
54
- ? $this->options['username']
55
- : 'user');
56
-
57
- $mail->setPassword(isset($this->options['password'])
58
- ? $this->options['password']
59
- : 'pa$$word');
60
-
61
- $mail->setPort(isset($this->options['port'])
62
- ? $this->options['port']
63
- : 25);
49
+ $mail->setHost($this->options['host'] ?? 'smtp.example.org');
50
+ $mail->setUsername($this->options['username'] ?? 'user');
51
+ $mail->setPassword($this->options['password'] ?? 'pa$$word');
52
+ $mail->setPort($this->options['port'] ?? 25);
64
53
65
54
// ...
66
55
}
67
56
}
68
57
69
- This boilerplate is hard to read and repetitive. Also, the default values of the
70
- options are buried in the business logic of your code. Use the
71
- :phpfunction: `array_replace ` to fix that::
58
+ Also, the default values of the options are buried in the business logic of your
59
+ code. Use the :phpfunction: `array_replace ` to fix that::
72
60
73
61
class Mailer
74
62
{
0 commit comments