Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

Define your own exception class

Mon Oct 27, 2008 11:52 pm

Code:

<?php
   
class InvalidEmailException extends Exception {
   
      function
__construct($message, $email) {
         
$this->message = $message;
         
$this->notifyAdmin($email);
      }

      
private function notifyAdmin($email) {
         
mail("[email protected]","INVALID EMAIL",$email,"From:[email protected]");
      }

   }

   class
subscribe {
      function
validateEmail($email) {
         
try {
            if (
$email == "") {
               
throw new Exception("You must enter an e-mail address!");
            } else {
               list(
$user,$domain) = explode("@", $email);
                  if (!
checkdnsrr($domain, "MX")) {
                     
throw new InvalidEmailException("Invalid e-mail address!", $email);
                  } else {
                     return
1;
                  }
            }
         }
catch (Exception $e) {
            echo
$e->getMessage();
         }
catch (InvalidEmailException $e) {
            echo
$e->getMessage();
         }
      }

      function
subscribeUser() {
         echo
$this->email." added to the database!";
      }

   }

   
$_POST['email'] = "[email protected]";

   if (isset(
$_POST['email'])) {
      
$subscribe = new subscribe();
      if(
$subscribe->validateEmail($_POST['email']))
         
$subscribe->subscribeUser($_POST['email']);
   }

?>




Post a reply
  Related Posts  to : Define your own exception class
 Define class helper class to check the method existance     -  
 Define class inside another class C++     -  
 define final class     -  
 define a Class with a Method     -  
 how to Define abstract class in php     -  
 Define Enum inside a class     -  
 Define getter and setter in php class     -  
 Define boolean Class properties     -  
 java abstract class,concrete class and interface     -  
 Define template in C++     -  

Topic Tags

PHP Exceptions