Skip to content

[OptionsResolver] Updated some example code to use modern PHP #11012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,
}
}

When accessing the ``$options``, you need to add a lot of boilerplate code to
When accessing the ``$options``, you need to add some boilerplate code to
check which options are set::

class Mailer
Expand All @@ -46,29 +46,17 @@ check which options are set::
{
$mail = ...;

$mail->setHost(isset($this->options['host'])
? $this->options['host']
: 'smtp.example.org');

$mail->setUsername(isset($this->options['username'])
? $this->options['username']
: 'user');

$mail->setPassword(isset($this->options['password'])
? $this->options['password']
: 'pa$$word');

$mail->setPort(isset($this->options['port'])
? $this->options['port']
: 25);
$mail->setHost($this->options['host'] ?? 'smtp.example.org');
$mail->setUsername($this->options['username'] ?? 'user');
$mail->setPassword($this->options['password'] ?? 'pa$$word');
$mail->setPort($this->options['port'] ?? 25);

// ...
}
}

This boilerplate is hard to read and repetitive. Also, the default values of the
options are buried in the business logic of your code. Use the
:phpfunction:`array_replace` to fix that::
Also, the default values of the options are buried in the business logic of your
code. Use the :phpfunction:`array_replace` to fix that::

class Mailer
{
Expand Down