Re: 5.4's New De-referencing plus assignment
| From: | Rasmus Lerdorf | Date: | Fri, 02 Dec 2011 16:56:41 +0000 |
| Subject: | Re: 5.4's New De-referencing plus assignment | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-56725@lists.php.net to get a copy of this message | ||
On 12/02/2011 08:50 AM, Matthew Weier O'Phinney wrote:
> if (!(($validator = new SomeValidator())->isValid($value))) {
> // Validation failed, get messages...
> $view->assign('errors' => $validator->getMessages());
> return $view->render('error');
> }
> // validation passed, do something...
>
> Yes, this could be written as follows:
>
> $validator = new SomeValidator();
> if (!$validator->isValid($value)) {
> // ...
> }
> // ...
>
> However, I can see some folks not really wanting that variable
> declaration if they won't be using it outside the conditional.
But $validator is still going to be defined regardless of the return
value of isValid() so it is going to be set outside the conditional.
-Rasmus