Organizational Research By

Surprising Reserch Topic

Question:Stop class execution without terminating the script (Or includes)


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:

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.
";
    private $msg        = 'Thank you for subscribing';
    protected $status   = true;

    function __construct() {
        self::validate();
        echo '
the CLASS continued
';
    }

    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

asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
0 votes
27 views



Related Hot Questions

8 Answers

0 votes
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;
}
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
No one stop you to just return boolean on validate method, then setup a simple if/else block to evaluate its result.

private function validate() {
    //Empty fields
    foreach ($_REQUEST as $key => $value) {
        $val = str_replace( ' ', '', $value );
        if ( $val === '' ) {
            // You shouldn't call staticaly
            // self::error( 'empty', $key );
            $this->error('empty', $key);
            // self::kill(); //If empty, this should end the loop and class
            return false;
        } //if:empty
    } //foreach

    //Validate Name
    if( !preg_match(self::exp_name,$_POST['Name']) ) {
        $this->error( 'name' );
        return false;
    }

    //Validate e-Mail
    if( !preg_match(self::exp_email,$_POST['e-Mail']) ) {
        $this->error( 'email' );
        return false;
    }

    return true;
}

Then in the constructor :

if ($this->validate()) {
    echo '
the CLASS continued
';
}
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

The problem is that there is no 'kill me' option in a class. PHP uses the unset method to destroy a variable, and that cannot be called from within a class. As the manual states 'It is not possible to unset $this inside an object method since PHP 5.', so you cannot kill your class.

That being said, how would you go about it then to make sure nothing happens when it doesnt validate? Make sure you check the status in each method of the class that shouldnt continue on error. Below you will find your code altered to show this process. Be sure to read the comments as to why I did something.

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.
"
; private $msg = 'Thank you for subscribing'; protected $status = true; function __construct() { //first check if it validates. //if it doesnt, end the constructor so the "class continued" line won't show. if (!self::validate()) { return; } echo '
the CLASS continued
'
; } private function validate() { //have the validate method return true/false. //Now you can use the output of this method to stop the script if needed //Empty fields foreach ($_REQUEST as $key => $value) { $val = str_replace( ' ', '', $value ); if ( $val === '' ) { self::error( 'empty', $key ); return false; //just return false when it doesnt validate } //if:empty
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

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:

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

The problem is that there is no 'kill me' option in a class. PHP uses the unset method to destroy a variable, and that cannot be called from within a class. As the manual states 'It is not possible to unset $this inside an object method since PHP 5.', so you cannot kill your class.

That being said, how would you go about it then to make sure nothing happens when it doesnt validate? Make sure you check the status in each method of the class that shouldnt continue on error. Below you will find your code altered to show this process. Be sure to read the comments as to why I did something.

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.
"
; private $msg = 'Thank you for subscribing'; protected $status = true; function __construct() { //first check if it validates. //if it doesnt, end the constructor so the "class continued" line won't show. if (!self::validate()) { return; } echo '
the CLASS continued
'
; } private function validate() { //have the validate method return true/false. //Now you can use the output of this method to stop the script if needed //Empty fields foreach ($_REQUEST as $key => $value) { $val = str_replace( ' ', '', $value ); if ( $val === '' ) { self::error( 'empty', $key ); return false; //just return false when it doesnt validate } //if:empty
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
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;
}
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
No one stop you to just return boolean on validate method, then setup a simple if/else block to evaluate its result.

private function validate() {
    //Empty fields
    foreach ($_REQUEST as $key => $value) {
        $val = str_replace( ' ', '', $value );
        if ( $val === '' ) {
            // You shouldn't call staticaly
            // self::error( 'empty', $key );
            $this->error('empty', $key);
            // self::kill(); //If empty, this should end the loop and class
            return false;
        } //if:empty
    } //foreach

    //Validate Name
    if( !preg_match(self::exp_name,$_POST['Name']) ) {
        $this->error( 'name' );
        return false;
    }

    //Validate e-Mail
    if( !preg_match(self::exp_email,$_POST['e-Mail']) ) {
        $this->error( 'email' );
        return false;
    }

    return true;
}

Then in the constructor :

if ($this->validate()) {
    echo '
the CLASS continued
';
}
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

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:

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...