{"id":47248,"date":"2024-12-02T15:48:42","date_gmt":"2024-12-02T07:48:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47248\/"},"modified":"2024-12-02T15:48:42","modified_gmt":"2024-12-02T07:48:42","slug":"php-%e5%87%bd%e6%95%b0%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47248\/","title":{"rendered":"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u5f02\u5e38\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u786e\u4fdd\u4e86\u6d4b\u8bd5\u7684\u5065\u58ee\u6027\u548c\u53ef\u9884\u6d4b\u6027\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4f7f\u7528 expectException() \u65ad\u8a00\u6765\u9a8c\u8bc1\u51fd\u6570\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u629b\u51fa\u9884\u671f\u7684\u5f02\u5e38\u5e76\u6b63\u786e\u5904\u7406\uff0c\u63d0\u9ad8\u4e86\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u6574\u4f53\u8d28\u91cf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729906787671c486397489.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u6280\u672f\uff0c\u7528\u4e8e\u9a8c\u8bc1\u5355\u4e2a\u4ee3\u7801\u5355\u5143\uff08\u5982\u51fd\u6570\u6216\u65b9\u6cd5\uff09\u7684\u6b63\u786e\u6027\u3002\u5f02\u5e38\u5904\u7406\u662f PHP \u4e2d\u5904\u7406\u9884\u671f\u548c\u610f\u5916\u9519\u8bef\u7684\u5173\u952e\u673a\u5236\u3002\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u6b63\u786e\u5904\u7406\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u6d4b\u8bd5\u7684\u5065\u58ee\u6027\u548c\u53ef\u9884\u6d4b\u6027\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a PHP \u51fd\u6570 <code>divide()<\/code>\uff0c\u8be5\u51fd\u6570\u5bf9\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u9664\u6cd5\uff0c\u5e76\u5728\u9664\u6570\u4e3a\u96f6\u65f6\u629b\u51fa\u4e00\u4e2a <code>DivisionByZeroError<\/code> \u5f02\u5e38\u3002<\/p>\n<pre>&lt;?php\nfunction divide($dividend, $divisor)\n{\n    if ($divisor == 0) {\n        throw new DivisionByZeroError(\"Cannot divide by zero\");\n    }\n\n    return $dividend \/ $divisor;\n}\n?&gt;<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u521b\u5efa\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\u6765\u6d4b\u8bd5\u51fd\u6570\u7684\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<pre>&lt;?php\nclass DivideTest extends \\PHPUnit\\Framework\\TestCase\n{\n    public function testDivideByZero()\n    {\n        $this-&gt;expectException(DivisionByZeroError::class);\n        divide(10, 0);\n    }\n}\n?&gt;<\/pre>\n<p>\u5728\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>expectException()<\/code> \u65ad\u8a00\u5728\u8c03\u7528 <code>divide()<\/code> \u51fd\u6570\u65f6\u4f1a\u629b\u51fa\u4e00\u4e2a <code>DivisionByZeroError<\/code> \u5f02\u5e38\u3002\u5982\u679c\u51fd\u6570\u4ee5\u4efb\u4f55\u5176\u4ed6\u65b9\u5f0f\u5931\u8d25\u6216\u6ca1\u6709\u629b\u51fa\u6240\u9700\u7684\u5f02\u5e38\uff0c\u6d4b\u8bd5\u5c31\u4f1a\u5931\u8d25\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u5728 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4f7f\u6211\u4eec\u80fd\u591f\u9a8c\u8bc1\u51fd\u6570\u7684\u9c81\u68d2\u6027\u548c\u9519\u8bef\u5904\u7406\u80fd\u529b\u3002\u901a\u8fc7\u4f7f\u7528 <code>expectException()<\/code> \u65ad\u8a00\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u629b\u51fa\u9884\u671f\u7684\u5f02\u5e38\u5e76\u6b63\u786e\u5904\u7406\u3002\u8fd9\u6837\u505a\u6709\u52a9\u4e8e\u63d0\u9ad8\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u6574\u4f53\u8d28\u91cf\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406 \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u5f02\u5e38\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u786e\u4fdd\u4e86\u6d4b\u8bd5\u7684\u5065\u58ee\u6027\u548c\u53ef\u9884\u6d4b\u6027\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4f7f\u7528 expectException() \u65ad\u8a00\u6765\u9a8c\u8bc1\u51fd\u6570\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u629b\u51fa\u9884\u671f\u7684\u5f02\u5e38\u5e76\u6b63\u786e\u5904\u7406\uff0c\u63d0\u9ad8\u4e86\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u6574\u4f53\u8d28\u91cf\u3002 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u5f02\u5e38\u5904\u7406 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u6280\u672f\uff0c\u7528\u4e8e\u9a8c\u8bc1\u5355\u4e2a\u4ee3\u7801\u5355\u5143\uff08\u5982\u51fd\u6570\u6216\u65b9\u6cd5\uff09\u7684\u6b63\u786e\u6027\u3002\u5f02\u5e38\u5904\u7406\u662f PHP \u4e2d\u5904\u7406\u9884\u671f\u548c\u610f\u5916\u9519\u8bef\u7684\u5173\u952e\u673a\u5236\u3002\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u6b63\u786e\u5904\u7406\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u6d4b\u8bd5\u7684\u5065\u58ee\u6027\u548c\u53ef\u9884\u6d4b\u6027\u81f3\u5173\u91cd\u8981\u3002 \u5b9e\u6218\u6848\u4f8b \u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a PHP \u51fd\u6570 divide()\uff0c\u8be5\u51fd\u6570\u5bf9\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u9664\u6cd5\uff0c\u5e76\u5728\u9664\u6570\u4e3a\u96f6\u65f6\u629b\u51fa\u4e00\u4e2a DivisionByZeroError \u5f02\u5e38\u3002 &lt;?php function divide($dividend, $divisor) { if ($divisor == 0) { throw new DivisionByZeroError(&#8220;Cannot divide by zero&#8221;); } return $dividend \/ $divisor; } ?&gt; \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u521b\u5efa\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\u6765\u6d4b\u8bd5\u51fd\u6570\u7684\u5f02\u5e38\u5904\u7406\uff1a &lt;?php class DivideTest extends \\PHPUnit\\Framework\\TestCase { public function testDivideByZero() [&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-47248","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47248","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=47248"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47248\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}