diff --git a/components/options_resolver.rst b/components/options_resolver.rst index e3b2ac56598..c0059e05260 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -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 @@ -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 {