Php 从被调用方返回(终止)调用方函数

Php 从被调用方返回(终止)调用方函数,php,Php,我想创建一个forward函数来终止调用者函数。 目前,我在被调用函数中使用“exit”来实现这一点 function caller (){ if($condition){ forward(); } //code that is not executet if $condition is true } //current solution function forward(){ //do something exit; } 调用forw

我想创建一个forward函数来终止调用者函数。 目前,我在被调用函数中使用“exit”来实现这一点

function caller (){
    if($condition){
        forward();
    }
    //code that is not executet if $condition is true
}

//current solution
function forward(){
    //do something
    exit;
}
调用forward函数后,我想省略return语句

function caller (){
    if($condition){
        forward();
        return; //this function exits, and no other lines inside this function are executed
    }
    //code that is not executet if $condition is true
}
我想除了抛出异常之外,没有其他方法了

谢谢您的帮助。

在“被调用”函数自行终止之前,您不能终止“调用者”函数

存在分配的调用堆栈和返回指针问题

我认为终止函数本身的最好方法是。。。。就这样结束吧

所以

在“被调用”函数自行终止之前,不能终止“调用方”函数

存在分配的调用堆栈和返回指针问题

我认为终止函数本身的最好方法是。。。。就这样结束吧

所以


有几种方法:

1) 在
if
info
else
branch之后包含代码

2) 正如您所提到的,抛出异常

3) 或者干脆做:

function caller (){
    if($condition){
        forward();
        return; // you return nothing (void, in PHP's case that would be NULL, I believe)
    }
    //code that is not executet if $condition is true
}

有几种方法:

1) 在
if
info
else
branch之后包含代码

2) 正如您所提到的,抛出异常

3) 或者干脆做:

function caller (){
    if($condition){
        forward();
        return; // you return nothing (void, in PHP's case that would be NULL, I believe)
    }
    //code that is not executet if $condition is true
}
“goto”可能是唯一的解决方案:

function post_to_host($url, $data, $cook, $ref, &$resp_head, $type=1)
         {$GLOBALS['POST_TO_HOST.TIME_START']=time();
          ...
          stream_set_timeout($fp, 20);
          fputs($fp, "Accept: */*\r\n"); if (check_timeout()) {goto timeout;}
          fputs($fp, "Accept-Language: *\r\n"); if (check_timeout()) {goto timeout;}
          fputs($fp, "Accept-Encoding: *\r\n"); if (check_timeout()) {goto timeout;}
          while (!feof($fp))
                {$line.=fgets($fp); if (check_timeout()) {goto timeout;}
                }
          return $line;

          timeout:
                  {return 'POST EXCEPTION: Total timeout!';
                  }
         }

function check_timeout()
         {return time()-$GLOBALS['POST_TO_HOST.TIME_START']>60;
         }
“goto”可能是唯一的解决方案:

function post_to_host($url, $data, $cook, $ref, &$resp_head, $type=1)
         {$GLOBALS['POST_TO_HOST.TIME_START']=time();
          ...
          stream_set_timeout($fp, 20);
          fputs($fp, "Accept: */*\r\n"); if (check_timeout()) {goto timeout;}
          fputs($fp, "Accept-Language: *\r\n"); if (check_timeout()) {goto timeout;}
          fputs($fp, "Accept-Encoding: *\r\n"); if (check_timeout()) {goto timeout;}
          while (!feof($fp))
                {$line.=fgets($fp); if (check_timeout()) {goto timeout;}
                }
          return $line;

          timeout:
                  {return 'POST EXCEPTION: Total timeout!';
                  }
         }

function check_timeout()
         {return time()-$GLOBALS['POST_TO_HOST.TIME_START']>60;
         }

抛出异常、使用exit或其他一些讨厌的东西,无论您如何看待它(如果您只想有条件地执行某些代码行),都是过火了

解决方案1

使用
else
语句。此函数将隐式返回
NULL
(在PHP中,默认情况下,退出函数时输入return语句将向调用者返回
NULL

解决方案2

return
语句将退出当前函数

function caller (){
    if($condition){
        forward();
        return; //this function exits, and no other lines inside this function are executed
    }
    //code that is not executet if $condition is true
}

在不传递和退出代码的情况下,不要使用
exit
终止脚本。Exit与die类似,只是没有输出到
php://stdout
(与echo的功能相同)它会将退出代码返回到操作系统(
exit
仅在CLI(命令行)模式下可用)。

抛出异常、使用exit或其他讨厌的东西都是过火的,不管你怎么看(如果您只想有条件地执行某些代码行)

解决方案1

使用
else
语句。此函数将隐式返回
NULL
(在PHP中,默认情况下,退出函数时输入return语句将向调用者返回
NULL

解决方案2

return
语句将退出当前函数

function caller (){
    if($condition){
        forward();
        return; //this function exits, and no other lines inside this function are executed
    }
    //code that is not executet if $condition is true
}

在不传递和退出代码的情况下,不要使用
exit
终止脚本。exit就像die一样,只是没有输出到
php://stdout
(与
echo
的功能相同)它将向操作系统返回退出代码(
exit
仅在CLI(命令行)模式下可用).

我只在评论中看到了这个答案,但在其他任何答案中都没有看到

总结,让我们考虑两种方法,以及为什么它们不适合前向功能:

  • 使用异常;异常不应用于流控制,它们用于处理异常事件(当操作失败时)

  • 使用goto;此语句只能用于在同一执行块内跳转,在本例中,该执行块将位于调用方的
    内,并且唯一的跳转目标位于函数的末尾

  • function caller (){
        if($condition){
            forward();
            return; //this function exits, and no other lines inside this function are executed
        }
        //code that is not executet if $condition is true
    }
    
  • 使用exit();这将完全终止脚本,不会让任何底层代码有机会执行任何其他操作。它通常用于某种致命错误,或者在少数情况下,设置一些HTTP头,然后阻止将更多输出发送到浏览器

  • 从技术上讲,转发呼叫基本上应该用来表示“他说什么就说什么”;有一个完美的解决方案:

    return forward();


    它将
    forward()
    的结果直接传递给调用
    caller()
    的代码,同时仍然遵守应用程序的堆栈执行顺序。

    我只在注释中看到了这个答案,但在其他任何答案中都没有看到

    总结,让我们考虑两种方法,以及为什么它们不适合前向功能:

  • 使用异常;异常不应用于流控制,它们用于处理异常事件(当操作失败时)

  • 使用goto;此语句只能用于在同一执行块内跳转,在本例中,该执行块将位于调用方的
    内,并且唯一的跳转目标位于函数的末尾

  • function caller (){
        if($condition){
            forward();
            return; //this function exits, and no other lines inside this function are executed
        }
        //code that is not executet if $condition is true
    }
    
  • 使用exit();这将完全终止脚本,不会让任何底层代码有机会执行任何其他操作。它通常用于某种致命错误,或者在少数情况下,设置一些HTTP头,然后阻止将更多输出发送到浏览器

  • 从技术上讲,转发呼叫基本上应该用来表示“他说什么就说什么”;有一个完美的解决方案:

    return forward();


    它将
    forward()
    的结果直接传递给调用
    caller()
    的代码,同时仍然尊重应用程序的堆栈执行顺序。

    可能是goto?但它非常丑陋。这就是例外情况。只要让“调用者”知道如何处理“被调用者”的结果就可以了(定义一个API和信号机制;例如空结果集)。从调用方退出kill是相当混乱的,除非在“异常”情况下;否则只能在非常特殊的情况下使用它。这个问题有什么了不起的地方,以至于人们