Re: 5.4's New De-referencing plus assignment

From: Date: Sat, 03 Dec 2011 09:42:18 +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-56732@lists.php.net to get a copy of this message
Hi!
     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.
Personally, I think the latter code is much better than the former. It makes clear what is created, where, and what the condition is doing, and doesn't require my internal parser to work too hard :) On the other hand, I don't see a real reason why -> shouldn't be able to be applied to any expression. Yes, sometimes it would be (or seem to me) bad style, so what? It's not like bad style is impossible now ;) -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

« previous php.internals (#56732) next »