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

This is only part of a thread. view whole thread
May 10, 2020 20:12 (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 

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.


Rowan Tommins (né Collins)