更有效的方式.php

发布于 2024-11-16 10:42:52 字数 1026 浏览 0 评论 0原文

我正在使用这段代码,但我知道这不是很有效。 还有别的办法吗?更有效率?

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疏忽 2024-11-23 10:42:52

这就是 异常 的用途:

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.
   //
}

当然,这意味着验证器类的方法抛出异常而不是返回布尔值

class Validator {

   function validate_age($form) {
      if (!is_numeric($form['age'])) throw new Exception('Invalid age.');
   }

   // 
   // .. etc ..
   //
}

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 ..
   //
}
长伴 2024-11-23 10:42:52

你可以提前退出..我不知道当出现故障时你的代码中到底发生了什么,但是如果这在一个函数中..你可以这样做而不是这样:你

if(condition1) {

  if (condition2) {

     return true;

  }

}

return false;

可以这样做:

if (!condition1) {
  return false;
}

if (!condition2) {
  return false;
}
return true;

所以你基本上处理了'else ' 首先是案例..

除此之外.. 这也可能有效:

if (condition1 && condition2 && condition3 && condition4) {
    return true;
}

或者:

if (
     condition1 && 
     condition2 && 
     condition3 && 
     condition4
  ) {
    return true;
} else {
    return false;
}

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;
}
旧瑾黎汐 2024-11-23 10:42:52

在这种非常具体的情况下,您可以使用 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);

这似乎非常合适,因为您确实进行了双重分配和 if 检查。

这是有效的,因为 and 的优先级低于 = 赋值运算符。您的 ===true 检查当然是多余的。如果您愿意,您可以将整个条件链重新打包为 if () 谓词。

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.

梦里°也失望 2024-11-23 10:42:52

看起来所有函数调用都返回一个布尔值。这段代码应该可以工作。如果任何调用返回 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);

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);
风月客 2024-11-23 10:42:52

将其包装在一个函数中,将所有检查转为负数,如果结果为负数,则从函数返回。
此外,您可以在 $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.

江南烟雨〆相思醉 2024-11-23 10:42:52

您可以在一个外部条件中检查所有验证,然后在其中打开一个数据库事务。将所有插入放入 try 中,并将事务回滚放入 catch 中。

像这样的事情:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文