
แปล : Español
ไลบรารี PHP สำหรับการจัดการข้อยกเว้น
ในการจัดการข้อยกเว้น คุณสามารถใช้ไลบรารีตัวจัดการข้อยกเว้น
ระบบปฏิบัติการ: Linux | หน้าต่าง
เวอร์ชัน PHP: 8.1.1
วิธีที่แนะนำในการติดตั้งส่วนขยายนี้คือผ่าน Composer
หากต้องการติดตั้ง ไลบรารี PHP ErrorHandler เพียง:
composer require josantonius/error-handlerคำสั่งก่อนหน้านี้จะติดตั้งเฉพาะไฟล์ที่จำเป็นเท่านั้น หากคุณต้องการ ดาวน์โหลดซอร์สโค้ดทั้งหมด คุณสามารถใช้:
composer require josantonius/error-handler --prefer-sourceคุณยังสามารถ โคลนพื้นที่เก็บข้อมูลทั้งหมด ด้วย Git:
git clone https://github.com/josantonius/php-error-handler.git JosantoniusErrorHandlerErrorException ขยาย ErrorException
รับไฟล์ข้อผิดพลาด:
public function getFile(): string ;รับระดับข้อผิดพลาด:
public function getLevel(): int ;รับบรรทัดไฟล์ข้อผิดพลาด:
public function getLine(): int ;ได้รับข้อความแสดงข้อผิดพลาด:
public function getMessage(): string ;รับชื่อข้อผิดพลาด:
public function getName(): string ; JosantoniusErrorHandlerErrorHandled
รับไฟล์ข้อผิดพลาด:
public function getFile(): string ;รับระดับข้อผิดพลาด:
public function getLevel(): int ;รับบรรทัดไฟล์ข้อผิดพลาด:
public function getLine(): int ;ได้รับข้อความแสดงข้อผิดพลาด:
public function getMessage(): string ;รับชื่อข้อผิดพลาด:
public function getName(): string ; JosantoniusErrorHandlerErrorHandler
แปลงข้อผิดพลาดเป็นข้อยกเว้น:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptions( int ... $ errorLevel ): ErrorHandler ;แปลงข้อผิดพลาดให้เป็นข้อยกเว้น ยกเว้นบางส่วน:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
public function convertToExceptionsExcept( int ... $ errorLevel ): ErrorHandler ;ลงทะเบียนฟังก์ชันตัวจัดการข้อผิดพลาด:
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
public function register( callable $ callback ): ErrorHandler ;ใช้การรายงานข้อผิดพลาดเพื่อพิจารณาว่าข้อผิดพลาดใดได้รับการจัดการ:
/**
* If the setting value in error_reporting() is used to determine which errors are handled.
*
* If this method is not used, all errors will be sent to the handler.
*
* @see https://www.php.net/manual/en/function.error-reporting.php
*/
public function useErrorReportingLevel(): ErrorHandler ; use Josantonius ErrorHandler Exceptions WrongErrorLevelException ;ตัวอย่างการใช้งานห้องสมุดนี้:
use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions. use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ( E_USER_ERROR , E_USER_WARNING );
// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions. use Josantonius ErrorHandler ErrorHandler ;
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptionsExcept ( E_USER_DEPRECATED , E_USER_NOTICE );
// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions. use Josantonius ErrorHandler ErrorHandler ;
error_reporting ( E_USER_ERROR );
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be converted to exception. use ErrorException ;
use Josantonius ErrorHandler ErrorHandler ;
set_exception_handler ( function ( ErrorException $ exception ) {
var_dump ([
' level ' => $ exception -> getLevel (),
' message ' => $ exception -> getMessage (),
' file ' => $ exception -> getFile (),
' line ' => $ exception -> getLine (),
' name ' => $ exception -> getName (),
]);
});
$ errorHandler = new ErrorHandler ();
$ errorHandler -> convertToExceptions ();
// All errors will be converted to exceptions. use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
function handler ( Errorhandled $ errorHandled ): void {
var_dump ([
' level ' => $ errorHandled -> getLevel (),
' message ' => $ errorHandled -> getMessage (),
' file ' => $ errorHandled -> getFile (),
' line ' => $ errorHandled -> getLine (),
' name ' => $ errorHandled -> getName (),
]);
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: handler (...)
);
// All errors will be converted to exceptions. use Josantonius ErrorHandler ErrorHandled ;
use Josantonius ErrorHandler ErrorHandler ;
class Handler {
public static function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ errorHandler = new ErrorHandler ();
$ errorHandler -> register (
callback: Handler:: errors (...)
)-> convertToExceptions ();
// The error will be sent to the error handler and then throw the exception. error_reporting ( E_USER_ERROR );
class Handler {
public function errors ( Errorhandled $ exception ): void { /* do something */ }
}
$ handler = new Handler ();
$ errorHandled -> register (
callback: $ handler -> errors (...),
)-> convertToExceptions ()-> useErrorReportingLevel ();
// Only E_USER_ERROR will be passed to the handler and converted to exception. หากต้องการรันการทดสอบ คุณเพียงแค่ต้องมีผู้แต่งและดำเนินการดังต่อไปนี้:
git clone https://github.com/josantonius/php-error-handler.git cd php-error-handler composer installรันการทดสอบหน่วยด้วย PHPUnit:
composer phpunitรันการทดสอบมาตรฐานโค้ดด้วย PHPCS:
composer phpcsรันการทดสอบ PHP Mess Detector เพื่อตรวจจับความไม่สอดคล้องกันในรูปแบบโค้ด:
composer phpmdรันการทดสอบก่อนหน้าทั้งหมด:
composer tests การเปลี่ยนแปลงโดยละเอียดสำหรับแต่ละรุ่นมีการบันทึกไว้ในบันทึกประจำรุ่น
โปรดตรวจสอบให้แน่ใจว่าได้อ่านคู่มือการมีส่วนร่วม ก่อนที่จะทำการดึงคำขอ เริ่มการสนทนาหรือรายงานปัญหา
ขอขอบคุณผู้มีส่วนร่วมทุกคน!
หากโครงการนี้ช่วยให้คุณลดเวลาในการพัฒนา คุณสามารถสนับสนุนฉันเพื่อสนับสนุนงานโอเพ่นซอร์สของฉันได้หรือไม่
พื้นที่เก็บข้อมูลนี้ได้รับอนุญาตภายใต้ใบอนุญาต MIT
ลิขสิทธิ์ © 2016-ปัจจุบัน, Josantonius