{"id":48528,"date":"2024-12-02T08:21:57","date_gmt":"2024-12-02T00:21:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48528\/"},"modified":"2024-12-02T08:21:57","modified_gmt":"2024-12-02T00:21:57","slug":"php%e5%87%bd%e6%95%b0%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48528\/","title":{"rendered":"php\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357<\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730074452671ed7541d5d9.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357\u63d2\u56fe\" alt=\"php\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u5bf9\u4e8e\u8bc6\u522b\u548c\u89e3\u51b3\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c\u4ecb\u7ecd\u5982\u4f55\u5904\u7406\u548c\u8c03\u8bd5 PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>1. \u542f\u7528\u9519\u8bef\u62a5\u544a<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPHP \u4e0d\u4f1a\u5728\u7f51\u9875\u4e0a\u663e\u793a\u9519\u8bef\u3002\u8981\u542f\u7528\u9519\u8bef\u62a5\u544a\uff0c\u8bf7\u5728 <code>php.ini<\/code> \u6587\u4ef6\u4e2d\u8bbe\u7f6e <code>display_errors<\/code> \u4e3a <code>On<\/code>\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>error_reporting()<\/code> \u51fd\u6570\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u542f\u7528\u62a5\u544a\u3002<\/p>\n<pre>\/\/ \u542f\u7528\u6240\u6709\u9519\u8bef\u62a5\u544a\nerror_reporting(E_ALL);<\/pre>\n<p><strong>2. \u5904\u7406\u9519\u8bef<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u5141\u8bb8\u81ea\u5b9a\u4e49\u5bf9\u9519\u8bef\u7684\u54cd\u5e94\u3002\u8981\u6ce8\u518c\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u8bf7\u4f7f\u7528 <code>set_error_handler()<\/code> \u51fd\u6570\u3002<\/p>\n<pre>\/\/ \u6ce8\u518c\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\nset_error_handler(function($errno, $errstr, $errfile, $errline) {\n  \/\/ \u5728\u6b64\u5904\u7406\u9519\u8bef\n});<\/pre>\n<p><strong>3. \u8c03\u8bd5\u9519\u8bef<\/strong><\/p>\n<p>\u4f7f\u7528\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u540e\uff0c\u53ef\u4ee5\u8c03\u8bd5\u9519\u8bef\u5e76\u627e\u51fa\u5176\u6839\u672c\u539f\u56e0\u3002\u53ef\u4ee5\u4f7f\u7528 <code>debug_backtrace()<\/code> \u51fd\u6570\u83b7\u53d6\u9519\u8bef\u7684\u8c03\u7528\u5806\u6808\u3002<\/p>\n<pre>\/\/ \u6253\u5370\u9519\u8bef\u8c03\u7528\u5806\u6808\nprint_r(debug_backtrace());<\/pre>\n<p><strong>4. \u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5728 <code>user.php<\/code> \u6587\u4ef6\u4e2d\u6709\u4e00\u4e2a <code>getUser()<\/code> \u51fd\u6570\uff0c\u5b83\u53ef\u80fd\u629b\u51fa <code>UserNotFoundException<\/code> \u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u6765\u5904\u7406\u548c\u8c03\u8bd5\u6b64\u5f02\u5e38\u3002<\/p>\n<pre>\/\/ user.php\nfunction getUser($id) {\n  \/\/ \u6a21\u62df\u7528\u6237\u4e0d\u5b58\u5728\u7684\u573a\u666f\n  if ($id === 1000) {\n    throw new UserNotFoundException();\n  }\n  \/\/ ...\n}\n\n\/\/ index.php\ntry {\n  $user = getUser(1000);\n} catch (UserNotFoundException $e) {\n  \/\/ \u5728\u6b64\u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p><strong>5. \u8c03\u8bd5\u9519\u8bef<\/strong><\/p>\n<p>\u8981\u8c03\u8bd5\u9519\u8bef\uff0c\u53ef\u4ee5\u6ce8\u518c\u4e00\u4e2a\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u5e76\u4f7f\u7528 <code>debug_backtrace()<\/code> \u51fd\u6570\u83b7\u53d6\u8c03\u7528\u5806\u6808\u3002<\/p>\n<pre>\/\/ user.php\nset_error_handler(function($errno, $errstr, $errfile, $errline) {\n  echo \"\u9519\u8bef: $errstr\\n\";\n  echo \"\u6587\u4ef6: $errfile\\n\";\n  echo \"\u884c\u53f7: $errline\\n\";\n  echo \"\u8c03\u7528\u5806\u6808:\\n\";\n  print_r(debug_backtrace());\n});<\/pre>\n<p>\u8fd0\u884c <code>index.php<\/code> \u6587\u4ef6\u65f6\uff0c\u5c06\u6253\u5370\u9519\u8bef\u8c03\u7528\u5806\u6808\uff0c\u5e2e\u52a9\u786e\u5b9a\u4ee3\u7801\u4e2d\u9519\u8bef\u7684\u6839\u6e90\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aphp\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357 \u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aphp\u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01 PHP \u51fd\u6570\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u6307\u5357 \u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u5bf9\u4e8e\u8bc6\u522b\u548c\u89e3\u51b3\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c\u4ecb\u7ecd\u5982\u4f55\u5904\u7406\u548c\u8c03\u8bd5 PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u3002 1. \u542f\u7528\u9519\u8bef\u62a5\u544a \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPHP \u4e0d\u4f1a\u5728\u7f51\u9875\u4e0a\u663e\u793a\u9519\u8bef\u3002\u8981\u542f\u7528\u9519\u8bef\u62a5\u544a\uff0c\u8bf7\u5728 php.ini \u6587\u4ef6\u4e2d\u8bbe\u7f6e display_errors \u4e3a On\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 error_reporting() \u51fd\u6570\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u542f\u7528\u62a5\u544a\u3002 \/\/ \u542f\u7528\u6240\u6709\u9519\u8bef\u62a5\u544a error_reporting(E_ALL); 2. \u5904\u7406\u9519\u8bef PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u5141\u8bb8\u81ea\u5b9a\u4e49\u5bf9\u9519\u8bef\u7684\u54cd\u5e94\u3002\u8981\u6ce8\u518c\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u8bf7\u4f7f\u7528 set_error_handler() \u51fd\u6570\u3002 \/\/ \u6ce8\u518c\u9519\u8bef\u5904\u7406\u7a0b\u5e8f set_error_handler(function($errno, $errstr, $errfile, $errline) { \/\/ \u5728\u6b64\u5904\u7406\u9519\u8bef }); 3. \u8c03\u8bd5\u9519\u8bef \u4f7f\u7528\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u540e\uff0c\u53ef\u4ee5\u8c03\u8bd5\u9519\u8bef\u5e76\u627e\u51fa\u5176\u6839\u672c\u539f\u56e0\u3002\u53ef\u4ee5\u4f7f\u7528 debug_backtrace() \u51fd\u6570\u83b7\u53d6\u9519\u8bef\u7684\u8c03\u7528\u5806\u6808\u3002 \/\/ \u6253\u5370\u9519\u8bef\u8c03\u7528\u5806\u6808 print_r(debug_backtrace()); 4. \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u5728 user.php \u6587\u4ef6\u4e2d\u6709\u4e00\u4e2a getUser() \u51fd\u6570\uff0c\u5b83\u53ef\u80fd\u629b\u51fa UserNotFoundException [&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-48528","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48528","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=48528"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48528\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}