i am using this code, but i know that is not very efficient. There is another way? more efficient ?
if ($val-> check($form) === true) {
{$data['livre'] = $val-> validate_age($form);}
if ($val->validate_age($form) === true) {
{$data['livre'] = $val->insertData($db, $form, $id);}
if ($val->insertData($db, $form, $id) === true) {
{$data['livre'] = $val->insertLanguages($db, $form, $id);}
if ($val->insertLanguages($db, $form, $id) === true) {
{$data['livre'] = $val->val($form);}
if ($val->val($form) === true) {
{$data['livre'] = $val->valexp($form);}
if ($val->valexp($form) === true ) {
{$data['l开发者_开发技巧ivre'] = $val->insertWorker($db, $form, $id);}
if ($val->insertWorker($db, $form, $id) === true) {
{$data['livre'] = $val->univAndCourse($form);}
...
thanks
That's what exceptions are for:
try {
$data['livre'] = $val->validate_age($form);
$data['livre'] = $val->insertData($db, $form, $id);
$data['livre'] = $val->insertLanguages($db, $form, $id);
$data['livre'] = $val->val($form);
$data['livre'] = $val->valexp($form);
$data['livre'] = $val->insertWorker($db, $form, $id);
$data['livre'] = $val->univAndCourse($form);
} catch (Exception $e) {
//
// Do what ever necessary to process the interrupted logic.
//
}
Of course, this implies that methods of the validator class throw exceptions
instead of returning booleans
:
class Validator {
function validate_age($form) {
if (!is_numeric($form['age'])) throw new Exception('Invalid age.');
}
//
// .. etc ..
//
}
You could exit early.. I don't know exactly what's happening in your code when there's a failure, but if this would be in a function.. you could do instead of this:
if(condition1) {
if (condition2) {
return true;
}
}
return false;
you could do:
if (!condition1) {
return false;
}
if (!condition2) {
return false;
}
return true;
So you basically handle the 'else' case first..
Besides that.. this may also work:
if (condition1 && condition2 && condition3 && condition4) {
return true;
}
or:
if (
condition1 &&
condition2 &&
condition3 &&
condition4
) {
return true;
} else {
return false;
}
In this very specific case you could be able to compress it into an expression using and
chaining:
$val-> check($form)
AND
$data['livre'] = $val-> validate_age($form)
AND
$data['livre'] = $val->insertData($db, $form, $id)
AND
$data['livre'] = $val->insertLanguages($db, $form, $id)
AND
$data['livre'] = $val->val($form)
AND
$data['livre'] = $val->valexp($form)
AND
$data['livre'] = $val->insertWorker($db, $form, $id);
Which seems very appropriate since you really double assigments and if
checks otherwise.
This works because and
has a lower precendence than the =
assigment operator. Your ===true
checks are certianly redundant. And if you wanted you could repackage that whole condition chain back as if ()
predicate.
It looks like all your function calls return a bool. This code should work. If any of the calls return false, $data['livre'] will be false.
$data['livre'] = $val->check($form) &&
$val->validate_age($form) &&
$val->insertData($db, $form, $id) &&
$val->insertLanguages($db, $form, $id) &&
$val->val($form) &&
$val->valexp($form) &&
$val->insertWorker($db, $form, $id) &&
$val->univAndCourse($form);
Wrap it in a function, turn all checks to negative and return from function, if the result is negative. Additionally, you can use exception inside the $val methods, so you will interupt the execution whenever there is an error, without checking each operation.
You could check all your validation in one outer conditional, open a database transaction inside of that. Put all your inserts inside of a try and a transaction rollback inside of a catch.
something like this:
if ($val-> check($form) === true && $val->validate_age($form) === true && $val->val($form) === true && $val->valexp($form) === true) {
//begin your transaction here. Depending on your framework it could be different.
mysql_query('start transaction');
try {
$val->insertData($db, $form, $id);
$val->insertLanguages($db, $form, $id);
$val->insertWorker($db, $form, $id);
//... blah blah blah more inserts here
//commit your transaction here
mysql_query('commit');
} catch (Exception $e) {
//Roll back the transaction here
mysql_query('rollback');
}
}
You just need to have your inserts throw an exception if they fail.
精彩评论