在下面的類中,我需要kill()來終止類中正在進行的任何操作,並且停止所有和類中的所有進程,而不是腳本:PHP:停止類的執行而不終止腳本(或包括)
<?php
class email {
//Expressions
const exp_name = "/^[A-Za-z .'-]+$/";
const exp_email = '/^[A-Za-z0-9._%-][email protected][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!!!';
?>
不可能。 PHP是單線程的。殺死它的一個線程會殺死所有的東西。您可以嘗試返回調用堆棧,直到您在對象「外部」,儘管我不確定如何觸發這樣的「深度」返回。 –
拋出異常? – datasage
我將需要示例,因爲我不是專家。 – Omar