PHP: stop class execution without ending script (or include)

In the following class, I need to kill () to finish everything that happens inside the class and just stop all and any processes in this 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!!!'; ?> 
+4
source share
6 answers

As stated in the comment above, try using the 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"); } 

Out of class:

 try { $mail = new email(); } catch (Exception $e) { echo $e->getMessage(); // Handle the message properly here. } 

See below for more details on the exception:

http://php.net/manual/en/language.exceptions.php

+10
source

Instead of self::kill(); just use return;

If you need kill to do other things, just use return self::kill(); when you need to call it and change kill to:

 private function kill() { doStuff(); return; } 
+1
source

Nobody stops you to just return a boolean using the validate method, and then set up 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 '<br/>the CLASS continued</b><br/>'; } 
0
source

If I understand this correctly, you can use return false instead of kill() . As far as I know, returning will β€œthrow” you beyond the bounds of the method, and execution will not be affected in any way. Sorry if I did not understand your problem correctly.

Best wishes!

0
source

The problem is that there is no β€œkill me” option in the class. PHP uses the unset method to destroy a variable and cannot be called from a class. Since the manual says "Cannot disable $ this inside an object method with PHP 5." so you cannot kill your class.

As the saying goes, how would you do this to make sure that nothing happens when it does not check? Make sure you check the status in each method of the class, which should not continue on error. Below you will find your code modified to show this process. Be sure to read the comments on 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.<br/>"; 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 '<br/>the CLASS continued</b><br/>'; } 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 } //foreach //Validate Name if( !preg_match(self::exp_name,$_POST['Name']) ) { self::error( 'name' ); return false; //just return false when it doesnt validate } //Validate e-Mail if( !preg_match(self::exp_email,$_POST['e-Mail']) ) { self::error( 'email' ); return false; //just return false when it doesnt validate } //else return true return true; } public function status() { //always allow the script to return the status return $this->status; } public function msg() { //always allow the script to return the error message return $this->msg; } //example function on how to make sure nothing happens on error public function send() { //return false on error so the rest method is not executed if ($this->status!==true) return false; //else continue echo "I'm sending an email!"; //mail(.....) } private function error( $type = null, $value = null ) { //you set the status to false here, so you can use it to check if an error occured //since you always set it to false, remove it from the switch statement. //This way there is less duplicate code and thus less chance for errors self::set( false ); switch( $type ) { case 'empty': $this->msg = self::error . "<div class='error'><b>The following field is empty: </b>" . $value . "</div>"; break; case 'name': $this->msg = self::error . "<div class='error'><b>The First Name you entered does not appear to be valid.</b></div>"; break; case 'email': $this->msg = self::error . "<div class='error'><b>The e-Mail you entered does not appear to be valid.</b></div>"; break; default: $this->msg = self::error; } return; } private function set( $boolean = false ) { //use a check to set the value to make sure its always a boolean //this way it will be false unless you actually set it to true. $this->status = ($boolean===true); } } $email = new email(); //now you can do a check for the message and only continue if there was no error if ($email->status() !== false) { //Do whatever you want with the e-mail $email->send(); echo 'An e-mail has been sent!'; } else { echo 'Something was wrong with the email ('.$email->msg().')'; } //or another approach could be to directly send it and catch if there was an error //make sure to check for false with === to be sure its realy a boolean. //If you would just check with == the method would also fail if the send() method would return 0 for instance if ($email->send() === false) { echo 'Something was wrong with the email ('.$email->msg().')'; } //but no matter what, the script will continue echo '<br/>'; echo '<br/>'; echo 'And the script continued!!!'; ?> 
0
source

You can do this programmatically as follows:

 class email { //Expressions public static $errorOccuredInCurrentRun = false; 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; public static function factory(){ //du your validation self::$errorOccuredInCurrentRun = false; self::validate(); if(!self::$errorOccuredInCurrentRun){ return new Email(); }else{ return null; } } private function __construct() { echo '<br/>the CLASS continued</b><br/>'; } private static function validate() { //Empty fields foreach ($_REQUEST as $key => $value) { if(self::$errorOccuredInCurrentRun){ break; } $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(self::$errorOccuredInCurrentRun){ if (!preg_match(self::exp_name, $_POST['Name'])) { self::error('name'); self::kill(); //kill //Validate e-Mail if(self::$errorOccuredInCurrentRun){ 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() { self::$validationRunCompleted; } } 

and then use it like this:

 $email = email::factory(); if(is_null($email)){ //something went wrong }else{ //validation was fine } 

i is executed only if the current "start" does not contain errors. And only if there were no errors, you yourself received an e-mail object so that you can continue and do what you need.

0
source

All Articles