【问题标题】:calling a method multiple times, is there a more productive way of doing it?多次调用一个方法,有没有更有效的方法呢?
【发布时间】:2012-07-08 03:41:11
【问题描述】:

我有一个用户注册脚本。在一个阶段,我将一个方法调用了 3 次。一次检查该方法是否返回 true,否则如果没有,它是否返回一个字符串(包含错误消息),如果它确实获取返回的字符串并将其放入变量中。

这是一种更高效的方法吗,这样我只需要调用一次该方法吗?但仍然得到我需要的所有答案?

代码如下:

//check thumbnail is present and good
            if($register->checkThumb()){
                //send image to permanent image directory
                $register->moveUploadedImage();

                //if the thumbnail failed validation put the error message in variable
            }else if(is_string($register->checkThumb())){
                $message = $register->checkThumb();

            }

【问题讨论】:

    标签: php class methods return


    【解决方案1】:
        $thumb = $register->checkThumb(); //call method once and save in variable
       /* using just if($thumb) would return always true, because 
          the function may returns an errormessage on failure 
          which is ja string, which is not empty, not 0, not false == true */
        if($thumb === true){
          //send image to permanent image directory
          $register->moveUploadedImage();
        }else{ //so then it's enough to ask for error this way
          $message = $thumb;
        }
    

    【讨论】:

      【解决方案2】:

      你可以在if语句中分配变量,

      if($checked = $register->checkThumb()){
          //send image to permanent image directory
          $register->moveUploadedImage();
      
          //if the thumbnail failed validation put the error message in variable
      }else if(is_string($checked)){
          $message = $checked;
      
      }
      

      【讨论】:

        【解决方案3】:

        你可以这样做:

        if(!($check_thumb_retvalue = $register->checkThumb())) {
          //send image to permanent image directory
          $register->moveUploadedImage();
        
        //if the thumbnail failed validation put the error message in variable
        }
        else if(is_string($check_thumb_retvalue)) {
          $message = $register->checkThumb();
        }
        

        或者,更具可读性:

        $check_thumb_retvalue = $register->checkThumb();
        if(!$check_thumb_retvalue){
          //send image to permanent image directory
          $register->moveUploadedImage();
        }
        //if the thumbnail failed validation put the error message in variable
        else if(is_string($check_thumb_retvalue)) {
          $message = $check_thumb_retvalue;
        }
        

        LG, CK

        【讨论】:

          【解决方案4】:

          你可以这样做:

                  $result = $register->checkThumb();
                  if($result){
                      //send image to permanent image directory
                      $register->moveUploadedImage();
          
                      //if the thumbnail failed validation put the error message in variable
                  }else if(is_string($result)){
                      $message = $result;
          
                  }
          

          但是你的代码很好,除非方法非常昂贵,否则根本不会有任何明显的区别。

          【讨论】:

            【解决方案5】:

            您可以将结果分配给变量,然后检查该变量。 此外,当您检查变量是否为真时,您应该使用运算符 === 进行检查。否则,如果函数返回非空字符串,它也将被限定为 true。运算符 === 检查类型,因此只有值为 true 的布尔变量才会通过。

            $result = $register->checkThumb();
            if($result === true) {
                $register->moveUploadedImage();
            } else if (is_string($result)){
                $message = $result;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-05-07
              • 2010-09-24
              • 2023-01-27
              • 2012-07-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多