|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-03-25 09:29 UTC] 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.
:-)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 22 21:00:01 2025 UTC |
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)Hi, If you're using an MVC pattern, you have too much logic in your controller. I would recommend you to create services for each step. Like this example: if (isset($_POST[...]) { $error = service1($_POST[...], $error = '... must not be empty'); if (!$error) { $error = service2(mysql.... , $error = 'Cannot reach database'); } if (!$error) { $error = service3(someting wrong, $error = 'wrong params'); } etc ... if (!$error) { // End } else { throw $error } } From my view the "break_if_empty" function will not be easier to read or understand since it's comparable to goto. It allows you to create spaghetti code the same way.Yes, there are others solutions, but none is so compact and simple. break_if_empty() is intended to be similar of "break" (for loops only), applied to current block "{ ...}", to maintain structured programming inside it like in loops. The add of the 2 parameters only enables most of the time to skip the require of new block "{ }". ("command" can be optional). It is exactely the same than (often used structure) : .... if (!expression) { [optionnal command;] goto End_of_block; } .... EndOfBlock: } ... <- continue here. but in only 1 line of code.