Advertisement
Guest User

PHP Exceptions vs Error classes

a guest
Jul 28th, 2010
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. class MyException extends Exception{}
  4. class MyException2 extends Exception{}
  5.  
  6. class BusinessLogicError{}
  7. class Error1 extends BusinessLogicError{}
  8. class Error2 extends BusinessLogicError{}
  9.  
  10. echo microtime(true) . '<br />';
  11. //7.64
  12. $i = 1;
  13. foreach(range(1, 1000000) as $k){
  14.     try{
  15.         throw new MyException2();
  16.     }
  17.     catch(MyException $e){
  18.         $i++;
  19.     }
  20.     catch(MyException2 $e){
  21.         $i++;
  22.     }
  23. }
  24.  
  25. echo microtime(true) . '<br />';
  26.  
  27. //1.88 sec
  28. $i = 0;
  29. foreach(range(1, 1000000) as $k){
  30.     $result = new Error2;
  31.    
  32.     if($result === true){
  33.         //ok   
  34.     }
  35.     else{
  36.         if($result instanceof Error1){
  37.             $i++;
  38.         }
  39.         elseif($result instanceof Error2){
  40.             $i++;
  41.         }  
  42.     }
  43. }  
  44.  
  45. echo microtime(true) . '<br />';
  46.  
  47. /**
  48. 1280303418.82
  49. 1280303426.49
  50. 1280303429.49
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement