In the class below, I need kill() to end whatever is going on within the class and just stop all and any processes WITHIN the class, not the script:
<?php
class email {
    //Expressions
    const exp_name      = "/^[A-Za-z .'-]+$/";
    const exp_email     = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    const error         = "We are sorry, but there appears to be a problem with the form you submitted.<br/>";
    private $msg        = 'Thank you for subscribing';
    protected $status   = true;
    function __construct() {
        self::validate();
        echo '<br/>the CLASS continued</b><br/>';
    }
    private function validate() {
        //Empty fields
        foreach ($_REQUEST as $key => $value) {
            $val = str_replace( ' ', '', $value );
            if ( $val === '' ) {
                self::error( 'empty', $key );
                self::kill(); //If empty, this should end the loop and class
            } //if:empty
        } //foreach
        //Validate Name
        if( !preg_match(self::exp_name,$_POST['Name']) ) {
            self::error( 'name' );
            self::kill(); //kill
        //Validate e-Mail
        if( !preg_match(self::exp_email,$_POST['e-Mail']) ) {
            self::error( 'email' );
            self::kill(); //kill
        }
  }
    public function status() {
        return $this->status;
    }
    public function msg() {
        return $this->msg;
    }
    private function error( $type = null, $value = null ) {
        switch( $type ) {
            case 'empty':
            $this->msg = self::error . "<div class='error'><b>The following field is empty: </b>" . $value . "</div>";
            self::set( false );
            break;
            case 'name':
            $this->msg = self::error . "<div class='error'><b>The First Name you entered does not appear to be valid.</b></div>";
            self::set( false );
            break;
            case 'email':
            $this->msg = self::error . "<div class='error'><b>The e-Mail you entered does not appear to be valid.</b></div>";
            self::set( false );
            break;
            default:
            self::set( false );
            $this->msg = self::error;
        }
    return; //kill app
    }
    private function set( $boolean = false ) {
        $this->status = $boolean;
    }
    private function kill() {
        //die();
        //exit( $this );
        //exit();
        //return;
        //break;
    }
}
$email = new email();
echo $email->msg();
echo '<br/>';
echo '<br/>';
echo 'The script continued!!!';
?>
                The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.
exit(): The function terminates the execution of a script.
You have to type ctrl + C to exit. Save this answer. Show activity on this post.
As answered above in a comment, try to use "try" and "catch" blocks.
Change the "validate" method
private function valdidate( $type = null, $value = null ) {
    // Anything that is going wrong:
    throw new Exception("Your error message");
}
Outside the class:
try {
      $mail = new email();
} catch (Exception $e) {
      echo $e->getMessage(); // Handle the message properly here.
}
For more info about exception see:
http://php.net/manual/en/language.exceptions.php
Instead of self::kill(); simply use return;
If you need kill to do other stuff then simply use return self::kill(); whenever you need to call it and change kill to:
private function kill() {
    doStuff();
    return;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With