Req #71896 [Com]: break to end of block if error

From: Date: Fri, 25 Mar 2016 10:55:17 +0000
Subject: Req #71896 [Com]: break to end of block if error
References: 1  Groups: php.standards 
Request: Send a blank email to standards-+get-1061@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71896&edit=1 ID: 71896 Comment by: inefedor at gmail dot com Reported by: p dot peyremorte at free dot fr Summary: break to end of block if error Status: Open Type: Feature/Change Request Package: PHP Language Specification Operating System: all PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Or you could throw an exception or do an early return, eg: if ( ! isset($_POST['field'])) { throw new InvalidInputException("field"); } or: if ( ! isset($_POST['field'])) { return new Result::error("Field error"); } Or separate extract from your code input data validation library, database library and so on, where each of them would tell you if they had an error in some way (either with exceptions or return values) Previous Comments: ------------------------------------------------------------------------ [2016-03-25 09:29:10] p dot peyremorte at free dot fr Description: ------------ Hi, I would like to propose to add a new instruction, like : break_if_empty(expression, command); Description: If "expression" is empty(undef or false), then execute "command" and skip until end of current block = skip until next "}" at same level. Explanation and example: Each time we have some linear treatment, i.e. on response to POST request, errors can occurs that must stop the linear treatment and return an error message, but not stop all the script. This produce many nested or successive checks of an "error flag" Exemple : if (isset($_POST[...]) { if (empty($_POST[...]) $error = '... must not be empty'; else { init something; if (!mysql....) $error = 'Cannot reach database'; else { .....; } } if (empty($error)) { if (someting wrong) $error = 'wrong params'; } if (empty($error)) { next steps... } etc ... } Such structure can be simplified by using function and "exit" or "return" but it requires to move the full bloc, and only it, in a function. All external variable must be passed as parameter or declared as global, inducing frequent miss or bug. It can also be simplifyed using "goto", but not so much and most of us really dislike it : if (isset($_POST[...]) { if (empty($_POST[...]) { $error = '... must not be empty'; goto EndBloc; } init something if (!mysql....) { $error = 'Cannot reach database'; goto EndBloc; } .....; if (empty($error)) goto EndBloc; next steps... EndBloc: } With break_if_empty(), my first exemple would resume to : if (isset($_POST[...]) { break_if_empty($_POST[...], $error = '... must not be empty'); init something; break_if_empty(mysql.... , $error = 'Cannot reach database'); .....; break_if_empty(someting wrong, $error = 'wrong params'); next steps... etc ... } This is a lot easier to read, to understand and to maintain than first exemple. :-) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71896&edit=1

« previous php.standards (#1061) next »