function bar() in D:\www\slim\public\index.php:7 • 因為 function 根本不存在,系統無從猜測可能的行為,也無法修復錯誤,故為 Fatal Error,強制停止程式運作。 function foo() { echo 'foo'; } bar(); // 呼叫了不存在的 function
with A::foo($a = 123) in D:\www\slim\public\index.php on line 17 對PHP開發過程更嚴謹的要求。 class A { public function foo($a = 123) { } } class B extends A { // 與 parent class 介面不一樣 public function foo() { } }
not A in D:\www\slim\public\index.php on line 6 • 可用的種類有: E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING, E_USER_DEPRECATED 等 大多數 E_* 的錯誤訊息,都有 E_USER_* 的對應 if ($a !== 'A') { trigger_error('$a is not A', E_USER_ERROR); }
Uncaught RuntimeException: $a is not A in D:\www\slim\public\index.php:5 Stack trace: #0 D:\www\slim\public\index.php(11): foo('B') #1 {main} thrown in D:\www\slim\public\index.php on line 5 function foo($a) { if ($a !== 'A') { throw new RuntimeException('$a is not A'); } echo $a; } foo('B');
{ case 'guest': // Please login first. case 'member': // Welcome Back case 'manager': // Sir, yes sir. default: throw new UnauthorisedException('Uh... Who are you?', 403); }
(!is_string($string)) { throw new InvalidArgumentException('Argument 1 should be string.'); } // 這個檢查比較鬆一點,只要是數字都可以過,不一定要 int 型態 if (!is_numeric($int)) { throw new InvalidArgumentException('Argument 2 should be a number.'); } // 這個檢查比較特別,如果是 Iterator 物件也能夠接受,因為同樣可以 foreach if (!is_array($array) && !($array instanceof Traversable)) { throw new InvalidArgumentException('Argument 3 should be Traversable.'); } // Do some stuff }