PHP与循环尝试赶上 [英] Php for loop with try catch

查看:107
本文介绍了PHP与循环尝试赶上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题从这个继续由用户之一。

我正在使用下面的getIDs函数来处理id的。 CheckValid()会检查这个id是否是有效的,如果是,那么它将进入下一个updateUsers()。检查有效的只是检查一个条件,如果没有,则会抛出异常。如果我通过getIDs()和execute()获得了4个id作为输出,那么updateUsers()只会更新一个列,如果它通过checkValid()。

它处理2例如,如果它失败的第二编号,它不会继续其余的2编号的..我希望它继续下去,所以我注释了在$ catch $抛出块。 ($ i = 0; $ i< count($ this->);

  getIDs()); $ i ++){
try {
$ this-> checkValid();
$ this-> updateUsers();
} catch(Exception $ e){
// throw $ e;


解决方案

听起来像是你使用异常作为布尔值,我建议避免这种情况,因为它很快就会混淆,除非你真的需要异常的内容。看看这是否对你的用例有任何意义(我会授予,它可能不)。

  //返回true if有效,否则返回false 
函数checkValid(){
try {
//执行验证
返回true;
} catch(Exception $ e){
//可选:保存异常以防我们想知道它
$ this-> last_error = $ e;
返回false;



函数execute(){
for($ i = 0; $ i< count($ this-> getIDs()); $如果($ this-> checkValid()){
$ this-> updateUsers();
}
//如果你想做一个错误,只需添加一个else子句
//并处理$ this-> last_error
}
}

另外,我显然不知道你的代码或者你在做什么,但是循环 n 次并调用 checkValid() updateUsers()参数似乎很差的做法。例如,更好地循环ID列表并检查每个ID和用户,如下所示: foreach($ this-> getIDs()as $ id){
if($ this-> checkValid($ id)){
$ this-> updateUser($ id);
} else {
//这样做的一个好处就是现在我们可以确切知道哪个ID失败了,
//因为我们有$ id变量
}
}


This question is in continuation from this as suggested by one of the user.

I am using the getIDs function as below to process the id's. CheckValid() will check if the id's is a valid one to be processed, if yes then it will go to the next one updateUsers(). Check valid just checks for a condition and if not it throws an exception. updateUsers() just updates a column if it passes checkValid().

Problem – If I get 4 id's as output from getIDs(), and with the execute(), it process 2 for example and if it fails for 2nd id, it doesn't continue for the rest 2 id's ..I want it to continue so I commented out the "throw $e in the catch block".

Function execute() { 
 for($i=0 ; $i<count($this->getIDs()); $i++) { 
try {
 $this->checkValid();
 $this->updateUsers(); 
} catch(Exception $e) {
  //throw $e;
}

解决方案

It sounds like you're using exceptions as booleans, I'd suggest avoiding that, as it gets confusing quickly, unless you really need the contents of the exception. See if this makes any sense for your use case (I'll grant, it may not).

// returns true if valid, false otherwise
function checkValid(){
    try {
        // do your validation
        return true;
    } catch (Exception $e) {
        // optional: save the exception in case we want to know about it
        $this->last_error = $e;
        return false;
    }
}

function execute() { 
    for($i=0 ; $i<count($this->getIDs()); $i++) { 
        if($this->checkValid()){
            $this->updateUsers();
        }
        // if you want to do something with an error, simply add an else clause
        // and handle $this->last_error
    }
}

Also, I obviously don't know your code or what exactly you're doing, but looping n times and calling checkValid() and updateUsers() without parameters seems like very poor practice. Much better to, for instance, loop over the list of IDs and check each ID and user in turn, something like this:

foreach($this->getIDs() as $id){
    if($this->checkValid($id)){
        $this->updateUser($id);
    } else {
        // an advantage of this is now we can know exactly which ID failed,
        // because we have the $id variable
    }
}

这篇关于PHP与循环尝试赶上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆