Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stop a PHP class from executing?

I'm looking for something like break for loops.

Here's some example code (using Symfony's lime) where stop() would not let the class continue and I_DONT_WANT_THIS_TO_RUN() would not be executed.

$browser->isStatusCode(200)
  ->isRequestParameter('module', 'home')
  ->isRequestParameter('action', 'index')
  ->click('Register')
  ->stop()
  ->I_DONT_WANT_THIS_TO_RUN();
$browser->thenThisRunsOkay();

Calling $this->__deconstruct(); from within stop() doesn't seem to do the trick. Is there a function I can call within stop() that would make that happen?

like image 856
thrashr888 Avatar asked Dec 19 '08 22:12

thrashr888


1 Answers

You could use PHP exceptions:

// This function would of course be declared in the class
function stop() {
    throw new Exception('Stopped.');
}

try {
    $browser->isStatusCode(200)
      ->isRequestParameter('module', 'home')
      ->isRequestParameter('action', 'index')
      ->click('Register')
      ->stop()
      ->I_DONT_WANT_THIS_TO_RUN();
} catch (Exception $e) {
    // when stop() throws the exception, control will go on from here.
}

$browser->thenThisRunsOkay();
like image 60
Paige Ruten Avatar answered Oct 05 '22 07:10

Paige Ruten