当sub函数中发生错误时,如何从主函数返回值

  • 本文关键字:函数 返回值 错误 sub php
  • 更新时间 :
  • 英文 :


我需要从名为concute();

的主函数返回值
class twCrawlerCommand extends ContainerAwareCommand
{
    public function test(){
        $some.... // error happnes!!
            return '1'//errorcode '1';
    }
    public function excute(){
        $returncode = $this->test();
        if ($returncode){return $returncode;}
    }
}

在此代码中,我可以在test()中的错误happnes;

中返回$ return Code

但是,如果功能巢更深或更复杂怎么办?

我认为通过$ return代码一对一可能有点笨拙。

有没有更好的方法将返回代码传递给主函数?

这样的事情怎么样:

class twCrawlerCommand extends ContainerAwareCommand {
    private errCode = 0;
    private function test() {
        $some.... // error happens!!
        $this->errCode = 1;
    }
    public function excute() {
        $this->test();
        if($this->errCode !== 0) {
            return $this->errCode;
            $this->errCode = 0; //reset
        }
    }
}

在这种情况下,您的最佳选择可能是例外。他们的意图之一就是您想要的。例如:

function foo($x)
{
   if($x)
   {
      throw new Exception($x);
   }
   return 0;
}
function bar($y)
{
   try
   {
      foo($y);
      //some other logic here. Useful part of function
      //should be placed here
      return 'well done';
   }
   catch(Exception $e)
   {
      //for example, in case that you'll want to get not just code
      return $e->getMessage();
   }
}
echo bar(-1);//-1
echo bar(0);//well done

- 您可以通过错误处理控制执行,并将逻辑放入try部分,从而处理catch部分中的不同错误。请注意,您可以捕获许多不同的例外 - 因此它可能是一个以上的catch块。

您应该看看异常。

那么您的代码看起来像这样

class twCrawlerCommand extends ContainerAwareCommand
{
    public function test(){
        throw new RuntimeException('Something wrong happened here');
    }
    public function excute(){
        try {
            $this->test();
        } catch (RuntimeException $e) {
            // handle exception with message in $e->getMessage()
        }
   }
}

您可以声明属性

class twCrawlerCommand extends ContainerAwareCommand
{
   public $last_error = false;
    public function test(){
        $some.... // error happnes!! 
            $this->last_error = "error description";
            return '1'//errorcode '1';
    }
    public function excute(){
        $returncode = $this->test();
        if ($returncode){return $returncode;}
    }
}

...班级外的某个地方:

if ($twCrawlerCommandInstance->execute() !== "0")
  echo("something bad happened: ".$twCrawlerCommandInstance->last_error);

您必须使用异常。像这样以某种方式:

class twCrawlerCommand extends ContainerAwareCommand
{
    public function test(){
        $some.... // error happnes!!
            throw new Exception('', 1);//errorcode '1';
    }
    public function excute(){
        $returncode = 0;
        try {
            $this->test();
        }
        catch (Exception $e) {
            $returncode = $e->getCode();
        }
        if ($returncode){return $returncode;}
    }
}

怎么样:

<?php
class twCrawlerCommand
{
    private $returncode = 0;
    public function test(){
            $this->returncode = 1;
    }
    public function execute(){
        $this->test();
        if (isset($this->returncode)){return $this->returncode;}
        return "";
    }
}
$foo = new twCrawlerCommand();
print $foo->execute() . "n";
?>

最新更新