Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard ClauseSyntax

This is only part of a thread. view whole thread
  110118
May 10, 2020 20:12 rowan.collins@gmail.com (Rowan Tommins)
Hi Ralph,

Like others, I think this feels too narrow in its scope, and doesn't add 
enough over existing syntax.

You mention that Ruby has a similar feature, but there it's not a 
special syntax for guard clauses per se, but a "modifier" form of the if 
statement: 
https://docs.ruby-lang.org/en/2.7.0/syntax/control_expressions_rdoc.html#label-Modifier+if+and+unless

Along with "unless" (an alias for "if not"), it's supposed to let you 
write more sentence-like code:

print "Hat Trick!" if $goals == 3;
throw new BadInputException unless is_valid($input);

In fact, Ruby also has modifier forms of while and until; and Perl 
(where I think this all came from) also for and foreach.


I'm not sure whether I'd welcome that syntax in PHP or not, but it would 
be one way to allow the throw, break, etc cases that other people have 
mentioned in addition to return, rather than having to invent special 
syntaxes for each.

Regards,

-- 
Rowan Tommins (né Collins)
[IMSoP]