PHP 7 ?? ??

PHP 7? ???? ??? ???? ??? ?????. PHP 5? ?? ?? ?? ????? ?? ???? ??? ?? Error ??? ?????.

?? ??? ?? ??? ?? ???? try/catch ???? catch? ? ????. ???? try / catch ??? ??? ??? ?? ?? ?? ??(set_Exception_handler()? ?? ???)? ?????. ?? ???? ???? ?? ?? ?? ???? ?????. ?, ???? ??? ?????.

Error ???? Exception ????? ???? ???? catch (Exception $e) { ... }? ?? ??? Error? ?? ? ????. catch (Error $e) { ... }? ?? ??? ????? ?? ???(set_Exception_handler())? ???? ??? ?? ? ????.

?? ?? ?? ??

  • Error
    • ArithmeticError
    • AssertionError
    • DivisionByZero ??
    • ParseError
    • TypeError
  • Exception
    • ...

Instance

<?php
class MathOperations 
{
   protected $n = 10;
   // 求余數(shù)運(yùn)算,除數(shù)為 0,拋出異常
   public function doOperation(): string
   {
      try {
         $value = $this->n % 0;
         return $value;
      } catch (DivisionByZeroError $e) {
         return $e->getMessage();
      }
   }
}
$mathOperationsObj = new MathOperations();
print($mathOperationsObj->doOperation());
?>

? ???? ??? ?? ??? ??? ????.

Modulo by zero