[英]more efficient way .php
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['livre'] = $val->insertWorker($db, $form, $id);}
if ($val->insertWorker($db, $form, $id) === true) {
{$data['livre'] = $val->univAndCourse($form);}
...
thanks 谢谢
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.. 所以你基本上先处理'else'案例..
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: 在这个非常特殊的情况下,您可以使用
and
链接将其压缩为表达式:
$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. 这似乎非常合适,因为你真的是双重配额,
if
检查。
This works because and
has a lower precendence than the =
assigment operator. 这是因为
and
比=
assigment运算符具有更低的优先级。 Your ===true
checks are certianly redundant. 你的
===true
检查是多余的。 And if you wanted you could repackage that whole condition chain back as if ()
predicate. 如果你想要你可以重新打包整个条件链,就像
if ()
谓词一样。
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
: 当然,这意味着验证器类的方法抛出
exceptions
而不是返回booleans
:
class Validator {
function validate_age($form) {
if (!is_numeric($form['age'])) throw new Exception('Invalid age.');
}
//
// .. etc ..
//
}
It looks like all your function calls return a bool. 看起来你的所有函数调用都返回一个bool。 This code should work.
这段代码应该有效。 If any of the calls return false, $data['livre'] will be false.
如果任何调用返回false,则$ data ['livre']将为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.
此外,您可以在$ val方法中使用异常,这样您就可以在出现错误时中断执行,而无需检查每个操作。
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.
将所有插入放在catch中的try和事务回滚中。
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. 如果它们失败,您只需要让插入抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.