Skip to content

Commit c555805

Browse files
committed
[OptionsResolver] Updated some example code to use modern PHP
1 parent 2234bd3 commit c555805

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

components/options_resolver.rst

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,
3636
}
3737
}
3838

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
4040
check which options are set::
4141

4242
class Mailer
@@ -46,29 +46,17 @@ check which options are set::
4646
{
4747
$mail = ...;
4848

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);
6453

6554
// ...
6655
}
6756
}
6857

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::
7260

7361
class Mailer
7462
{

0 commit comments

Comments
 (0)