{"id":48242,"date":"2024-12-02T08:07:11","date_gmt":"2024-12-02T00:07:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48242\/"},"modified":"2024-12-02T08:07:11","modified_gmt":"2024-12-02T00:07:11","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48242\/","title":{"rendered":"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u5728\u51fd\u6570\u5f0f PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u901a\u8fc7\u5f02\u5e38\u6216\u56de\u8c03\u51fd\u6570\u8fdb\u884c\uff0c\u5f02\u5e38\u5904\u7406\u7528\u4e8e\u6355\u83b7\u9884\u671f\u9519\u8bef\uff0c\u800c\u56de\u8c03\u51fd\u6570\u5904\u7406\u610f\u5916\u9519\u8bef\u3002\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7 try-catch \u8bed\u53e5\u6355\u83b7\uff0c\u56de\u8c03\u51fd\u6570\u53ef\u901a\u8fc7 set_error_handler() \u51fd\u6570\u8bbe\u7f6e\uff0c\u7528\u4e8e\u9519\u8bef\u53d1\u751f\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002debug_backtrace() \u51fd\u6570\u53ef\u63d0\u4f9b\u9519\u8bef\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730004463671dc5efa7658.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u5c24\u4e3a\u91cd\u8981\u3002PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9519\u8bef\u5904\u7406\u5de5\u5177\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u548c\u56de\u8c03\u51fd\u6570\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002<\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5f02\u5e38\u662f\u5bf9\u7a0b\u5e8f\u4e2d\u53d1\u751f\u95ee\u9898\u7684\u6b63\u5f0f\u8868\u793a\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u4e2d\u65ad\u7a0b\u5e8f\u6d41\u5e76\u5411\u7528\u6237\u63d0\u4f9b\u4fe1\u606f\u3002PHP \u4e2d\u6709\u4e24\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff1a<\/p>\n<pre>class Exception {}\nclass Error {}<\/pre>\n<p>\u6240\u6709\u9519\u8bef\u90fd\u4f1a\u8f6c\u6362\u4e3a\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>try-catch<\/code> \u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n  \/\/ \u5bb9\u6613\u51fa\u9519\u7684\u4ee3\u7801\n} catch (Exception $e) {\n  \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p><strong>\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5728\u9519\u8bef\u53d1\u751f\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002PHP \u7684 <code>set_error_handler()<\/code> \u51fd\u6570\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u9519\u8bef\uff1a<\/p>\n<pre>function errorHandler($errno, $errstr, $errfile, $errline) {\n  \/\/ \u5904\u7406\u9519\u8bef\n}\n\nset_error_handler('errorHandler');<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u5f02\u5e38\u548c\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u9519\u8bef\u5904\u7406\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\nfunction divide($a, $b) {\n  if ($b == 0) {\n    throw new Exception('Division by zero');\n  }\n  return $a \/ $b;\n}\n\ntry {\n  echo divide(10, 5);\n  echo divide(10, 0);\n} catch (Exception $e) {\n  echo $e-&gt;getMessage();\n}\n\nset_error_handler(function($errno, $errstr, $errfile, $errline) {\n  echo \"Error: $errstr in $errfile on line $errline\";\n});\n\ntrigger_error('Custom error', E_USER_NOTICE);\n\n?&gt;<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>2\nDivision by zero\nError: Custom error in index.php on line 25<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406 <code>0<\/code> \u9664\u6cd5\u7684\u9519\u8bef\u3002\u9519\u8bef\u5904\u7406\u56de\u8c03\u51fd\u6570\u5219\u7528\u4e8e\u5904\u7406\u81ea\u5b9a\u4e49\u9519\u8bef\u3002<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406\u9884\u671f\u9519\u8bef\u3002<\/li>\n<li>\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u610f\u5916\u9519\u8bef\u3002<\/li>\n<li><code>debug_backtrace()<\/code> \u51fd\u6570\u53ef\u7528\u4e8e\u83b7\u53d6\u9519\u8bef\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5 \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u5728\u51fd\u6570\u5f0f PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u901a\u8fc7\u5f02\u5e38\u6216\u56de\u8c03\u51fd\u6570\u8fdb\u884c\uff0c\u5f02\u5e38\u5904\u7406\u7528\u4e8e\u6355\u83b7\u9884\u671f\u9519\u8bef\uff0c\u800c\u56de\u8c03\u51fd\u6570\u5904\u7406\u610f\u5916\u9519\u8bef\u3002\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7 try-catch \u8bed\u53e5\u6355\u83b7\uff0c\u56de\u8c03\u51fd\u6570\u53ef\u901a\u8fc7 set_error_handler() \u51fd\u6570\u8bbe\u7f6e\uff0c\u7528\u4e8e\u9519\u8bef\u53d1\u751f\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002debug_backtrace() \u51fd\u6570\u53ef\u63d0\u4f9b\u9519\u8bef\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5 \u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u5c24\u4e3a\u91cd\u8981\u3002PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9519\u8bef\u5904\u7406\u5de5\u5177\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u548c\u56de\u8c03\u51fd\u6570\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002 \u5f02\u5e38\u5904\u7406 \u5f02\u5e38\u662f\u5bf9\u7a0b\u5e8f\u4e2d\u53d1\u751f\u95ee\u9898\u7684\u6b63\u5f0f\u8868\u793a\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u4e2d\u65ad\u7a0b\u5e8f\u6d41\u5e76\u5411\u7528\u6237\u63d0\u4f9b\u4fe1\u606f\u3002PHP \u4e2d\u6709\u4e24\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff1a class Exception {} class Error {} \u6240\u6709\u9519\u8bef\u90fd\u4f1a\u8f6c\u6362\u4e3a\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 try-catch \u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\uff1a try { \/\/ \u5bb9\u6613\u51fa\u9519\u7684\u4ee3\u7801 } catch (Exception $e) { \/\/ \u5904\u7406\u5f02\u5e38 } \u56de\u8c03\u51fd\u6570 \u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5728\u9519\u8bef\u53d1\u751f\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002PHP \u7684 set_error_handler() \u51fd\u6570\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u9519\u8bef\uff1a function errorHandler($errno, $errstr, $errfile, $errline) { \/\/ \u5904\u7406\u9519\u8bef } set_error_handler(&#8216;errorHandler&#8217;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48242","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48242"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48242\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}