更有效的方式.php
我正在使用这段代码,但我知道这不是很有效。 还有别的办法吗?更有效率?
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);}
...
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是 异常 的用途:
当然,这意味着验证器类的方法抛出
异常
而不是返回布尔值
:That's what exceptions are for:
Of course, this implies that methods of the validator class throw
exceptions
instead of returningbooleans
:你可以提前退出..我不知道当出现故障时你的代码中到底发生了什么,但是如果这在一个函数中..你可以这样做而不是这样:你
可以这样做:
所以你基本上处理了'else ' 首先是案例..
除此之外.. 这也可能有效:
或者:
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:
you could do:
So you basically handle the 'else' case first..
Besides that.. this may also work:
or:
在这种非常具体的情况下,您可以使用
and
链接将其压缩为表达式:这似乎非常合适,因为您确实进行了双重分配和
if
检查。这是有效的,因为
and
的优先级低于=
赋值运算符。您的===true
检查当然是多余的。如果您愿意,您可以将整个条件链重新打包为if ()
谓词。In this very specific case you could be able to compress it into an expression using
and
chaining: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 asif ()
predicate.看起来所有函数调用都返回一个布尔值。这段代码应该可以工作。如果任何调用返回 false,则 $data['livre'] 将为 false。
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.
将其包装在一个函数中,将所有检查转为负数,如果结果为负数,则从函数返回。
此外,您可以在 $val 方法中使用异常,因此只要出现错误,您就会中断执行,而无需检查每个操作。
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.
您可以在一个外部条件中检查所有验证,然后在其中打开一个数据库事务。将所有插入放入 try 中,并将事务回滚放入 catch 中。
像这样的事情:
你只需要让你的插入在失败时抛出异常。
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:
You just need to have your inserts throw an exception if they fail.