php
  • class
  • 2013-02-05 199 views 4 likes 
    4

    在下面的類中,我需要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!!!'; 
    ?> 
    
    +0

    不可能。 PHP是單線程的。殺死它的一個線程會殺死所有的東西。您可以嘗試返回調用堆棧,直到您在對象「外部」,儘管我不確定如何觸發這樣的「深度」返回。 –

    +0

    拋出異常? – datasage

    +0

    我將需要示例,因爲我不是專家。 – Omar

    回答

    10

    正如上面在評論中回答的,嘗試使用「try」和「catch」塊。

    更改 「驗證」 方法

    private function valdidate($type = null, $value = null) { 
        // Anything that is going wrong: 
        throw new Exception("Your error message"); 
    } 
    

    類外:

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

    有關異常的詳細信息,請參閱:

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

    1

    而不是self::kill();只需使用return;

    如果需要kill做其他的東西,然後只需使用return self::kill();當你需要調用它,改變kill到:

    private function kill() { 
        doStuff(); 
        return; 
    } 
    
    0

    沒有人阻止你只是回報validate方法,然後設置一個簡單的布爾if/else塊來評估其結果。

    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; 
    } 
    

    然後在構造函數中:

    if ($this->validate()) { 
        echo '<br/>the CLASS continued</b><br/>'; 
    } 
    
    0

    如果我理解正確此相反kill()可以使用return false。據我所知,return會拋出方法外,並且執行不會受到任何影響。如果我沒有正確理解你的問題,請聯繫我。

    最好的問候!

    +0

    我想要的是......當類郵件使用kill()時,只需完全停止ITSELF繼續運行(以「殺死」類郵件)並允許[php]腳本繼續運行。在這個例子中,'echo $ email-> msg(); echo'
    '; echo'
    '; echo'The script continued !!!';' – Omar

    +1

    是的,我知道你應該刪除'kill();'方法,並用'return'替換它,它將退出該方法。作爲替代方法,您可以返回false並使用條件語句來比較返回的結果,如: 'if(self :: validate()!== false){// do something}'這可以解決您的問題(至少在理論上那是的) –

    +0

    Yeaaa ......但是,目標依然存在,只停留在課堂上。 – Omar

    0

    問題在於班級中沒有「殺了我」選項。 PHP使用unset方法來銷燬變量,並且不能從類中調用該變量。正如手冊所述'自PHP 5以來,不可能在對象方法中取消設置$',所以你不能殺死你的類。

    這就是說,你會如何去做,然後確保什麼也沒有發生時,它不驗證?確保你檢查了不應該繼續出錯的每個類的方法中的狀態。下面你會發現你的代碼被改變來顯示這個過程。請務必閱讀關於我爲什麼做了什麼的評論。

    <?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() { 
         //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

    你可以做到這一點編程方式是這樣的:

    class email { 
        //Expressions 
        public static $errorOccuredInCurrentRun = false; 
    
        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; 
    
        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; 
        } 
    
    } 
    

    ,然後用它這樣的:

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

    如果當前的「運行」沒有,我只執行的東西任何錯誤。只有在沒有錯誤的情況下,您才能獲得電子郵件對象本身,並執行您所需要的操作。

    相關問題