Paul
Paul

Reputation: 1400

replace die to stop process

I'm using die to stop the process when I get an error:

main class:

class myClass {

    public function myMethod () {

        $helper->checkParameter( $thisParameterCanNotBeEmpty );

    }

}

and my helper

class control {

    public function checkParameter ( $param ) {

        if ( $param == "" ) {

            echo "parameter can not be empty";
            die;

        }

    }

}

like that if the parameter is empty I have an error message and the process is stopped.

But my problem is now I want to use unit tests (with PHPUnit) and the die stop the process (ok) and the unit tests execution (not ok).

With Zend 1.12, is it possible to stop the process with calling view or something instead of kill all the php process ?

Thx

Upvotes: 0

Views: 53

Answers (2)

bibliophilsagar
bibliophilsagar

Reputation: 1753

Instead of

if($param == "") {
}

you can use

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

It's more accurate.

Upvotes: 1

Stanimir Dimitrov
Stanimir Dimitrov

Reputation: 1890

Like @Leggendario said, use must use exceptions.

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

The \ before Exception tells PHP to use its built in namespace for the exception.

Upvotes: 0

Related Questions