更换模具以停止过程

replace die to stop process

我在遇到错误时使用 die 停止进程:

主要class:

class myClass {

    public function myMethod () {

        $helper->checkParameter( $thisParameterCanNotBeEmpty );

    }

}

还有我的帮手

class control {

    public function checkParameter ( $param ) {

        if ( $param == "" ) {

            echo "parameter can not be empty";
            die;

        }

    }

}

就像这样,如果参数为空,我会收到一条错误消息,进程会停止。

但我现在的问题是我想使用单元测试(使用 PHPUnit)并且模具停止进程(正常)和单元测试执行(不正常)。

使用 Zend 1.12,是否可以通过调用视图或其他方式停止进程而不是杀死所有 php 进程?

感谢

就像@Leggendario说的,使用必须使用异常

class control
{
    public function checkParameter ( $param )
    {
        if ( $param == "" ) {
            throw new \Exception("parameter can not be empty");
        }
    }
}

Exception 之前的 \ 告诉 PHP 使用其内置的命名空间来处理异常。

而不是

if($param == "") {
}

你可以使用

    if(!empty($param)) {
    //your normal code flow
    } else {
    throw new \Exception("parameter can not be empty");
    }

更准确。