Re: 5.4's New De-referencing plus assignment
| From: | Matthew Weier O'Phinney | Date: | Fri, 02 Dec 2011 17:07:42 +0000 |
| Subject: | Re: 5.4's New De-referencing plus assignment | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-56727@lists.php.net to get a copy of this message | ||
On 2011-12-02, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:
> 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.
True. My point was that _semantically_ it looks like it's contained by
the conditional. _Technically_, it's not.
My main point is that this looks like a pattern folks will try
immediately, and wonder why it doesn't work. Whether or not it _should_
work, I'm ambivalent about. I'm more likely to declare and then invoke.
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc