{"id":48169,"date":"2024-12-02T08:09:52","date_gmt":"2024-12-02T00:09:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48169\/"},"modified":"2024-12-02T08:09:52","modified_gmt":"2024-12-02T00:09:52","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%e9%94%99%e8%af%af%e6%b6%88%e6%81%af%e8%a7%a3%e8%af%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48169\/","title":{"rendered":"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\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\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\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\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>\u7b54\u6848\uff1a PHP \u5355\u5143\u6d4b\u8bd5\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u6709\u52a9\u4e8e\u8bc6\u522b\u548c\u4fee\u590d\u6d4b\u8bd5\u5931\u8d25\u7684\u539f\u56e0\uff0c\u5305\u62ec\uff1a1. \u53c2\u6570\u9519\u8bef\uff1a\u6307\u793a\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u6b63\u786e\u30022. \u8fd4\u56de\u503c\u9519\u8bef\uff1a\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0e\u9884\u671f\u7c7b\u578b\u6216\u503c\u4e0d\u5339\u914d\u30023. \u5f02\u5e38\uff1a\u6307\u793a\u51fd\u6570\u5185\u90e8\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u548c\u6d88\u606f\u30024. \u6bd4\u8f83\u5931\u8d25\uff1a\u6307\u51fa\u9884\u671f\u503c\u548c\u51fd\u6570\u8f93\u51fa\u4e0d\u5339\u914d\u7684\u539f\u56e0\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729994815671da03f21ec4.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb<\/strong><\/p>\n<p>\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u662f\u4fdd\u8bc1 PHP \u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u5b9e\u8df5\u3002\u9519\u8bef\u6d88\u606f\u5bf9\u4e8e\u7406\u89e3\u6d4b\u8bd5\u5931\u8d25\u7684\u539f\u56e0\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u6b64\u6b63\u786e\u89e3\u8bfb\u8fd9\u4e9b\u6d88\u606f\u5bf9\u4e8e\u8c03\u8bd5\u548c\u4fee\u590d\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>1. \u53c2\u6570\u9519\u8bef<\/strong><\/p>\n<p>\u9519\u8bef\u6d88\u606f\u901a\u5e38\u4f1a\u6307\u793a\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u6b63\u786e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Fatal error: Uncaught TypeError: Argument 1 passed to myFunction() must be of the type string, integer given<\/pre>\n<p>\u6b64\u9519\u8bef\u8868\u793a <code>myFunction()<\/code> \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u6574\u6570\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4fee\u6539\u8c03\u7528\u4ee3\u7801\u4ee5\u63d0\u4f9b\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>2. \u8fd4\u56de\u503c\u9519\u8bef<\/strong><\/p>\n<p>\u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u6307\u793a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0d\u5339\u914d\u9884\u671f\u7684\u7c7b\u578b\u6216\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Failed asserting that function \"myFunction\" returns type \"int\".<\/pre>\n<p>\u6b64\u9519\u8bef\u8868\u793a <code>myFunction()<\/code> \u9884\u8ba1\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd4\u56de\u4e86\u4e00\u4e2a\u5176\u4ed6\u7c7b\u578b\u6216\u4e00\u4e2a\u610f\u5916\u7684\u503c\u3002\u68c0\u67e5\u51fd\u6570\u5b9a\u4e49\u4ee5\u786e\u4fdd\u5b83\u8fd4\u56de\u9884\u671f\u7c7b\u578b\u548c\u503c\u3002<\/p>\n<p><strong>3. \u5f02\u5e38<\/strong><\/p>\n<p>\u51fd\u6570\u53ef\u4ee5\u5728\u5185\u90e8\u629b\u51fa\u5f02\u5e38\u3002\u9519\u8bef\u6d88\u606f\u5c06\u6307\u793a\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u548c\u6d88\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>PHP Fatal error:  Uncaught exception 'Exception' with message<\/pre>\n<p>\u68c0\u67e5\u51fd\u6570\u4ee3\u7801\u4ee5\u89e3\u51b3\u5f02\u5e38\u5e76\u4fee\u590d\u9519\u8bef\u3002<\/p>\n<p><strong>4. \u6bd4\u8f83\u5931\u8d25<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u7ecf\u5e38\u6bd4\u8f83\u9884\u671f\u503c\u548c\u51fd\u6570\u8f93\u51fa\u3002\u9519\u8bef\u6d88\u606f\u5c06\u6307\u793a\u6bd4\u8f83\u5931\u8d25\u7684\u786e\u5207\u539f\u56e0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Failed asserting that expected value \"Hello\" matches actual value \"world\".<\/pre>\n<p>\u786e\u4fdd\u6bd4\u8f83\u7684\u9884\u671f\u503c\u4e0e\u51fd\u6570\u8f93\u51fa\u76f8\u5339\u914d\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u4e00\u4e2a <code>calculateArea()<\/code> \u51fd\u6570\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u53ef\u80fd\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\uff1a<\/p>\n<pre>\n&lt;?php\n\nuse PHPUnit\\Framework\\TestCase;\n\nclass CalculateAreaTest extends TestCase\n{\n    public function testCalculateArea()\n    {\n        $length = 10;\n        $width = 5;\n\n        $area = calculateArea($length, $width);\n\n        $this-&gt;assertEquals(50, $area);\n    }\n}\n\nfunction calculateArea($length, $width)\n{\n    if (!is_numeric($length) || !is_numeric($width)) {\n        throw new Exception(\"Arguments must be numeric\");\n    }\n\n    return $length * $width;\n}<\/pre>\n<p><strong>\u53ef\u80fd\u7684\u9519\u8bef\u6d88\u606f\u548c\u89e3\u8bfb\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p><strong>PHP Fatal error: Uncaught exception &#8216;Exception&#8217; with message &#8216;Arguments must be numeric&#8217;<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u7684 <code>length<\/code> \u6216 <code>width<\/code> \u53c2\u6570\u4e0d\u662f\u6570\u5b57\uff0c\u9700\u8981\u4fee\u6539\u8c03\u7528\u4ee3\u7801\u4ee5\u63d0\u4f9b\u6570\u5b57\u53c2\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Failed asserting that expected value &#8220;50&#8221; matches actual value &#8220;100&#8221;.<\/strong><\/p>\n<ul>\n<li>\u8ba1\u7b97\u9762\u79ef\u4e0d\u6b63\u786e\uff0c\u9700\u8981\u68c0\u67e5 <code>calculateArea()<\/code> \u51fd\u6570\u7684\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7406\u89e3\u9519\u8bef\u6d88\u606f\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u8bc6\u522b\u548c\u4fee\u590d PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u5355\u5143\u6d4b\u8bd5\u7684\u8d44\u6599\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\u9519\u8bef\u6d88\u606f\u89e3\u8bfb \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u7b54\u6848\uff1a PHP \u5355\u5143\u6d4b\u8bd5\u9519\u8bef\u6d88\u606f\u89e3\u8bfb\u6709\u52a9\u4e8e\u8bc6\u522b\u548c\u4fee\u590d\u6d4b\u8bd5\u5931\u8d25\u7684\u539f\u56e0\uff0c\u5305\u62ec\uff1a1. \u53c2\u6570\u9519\u8bef\uff1a\u6307\u793a\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u6b63\u786e\u30022. \u8fd4\u56de\u503c\u9519\u8bef\uff1a\u8868\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0e\u9884\u671f\u7c7b\u578b\u6216\u503c\u4e0d\u5339\u914d\u30023. \u5f02\u5e38\uff1a\u6307\u793a\u51fd\u6570\u5185\u90e8\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u548c\u6d88\u606f\u30024. \u6bd4\u8f83\u5931\u8d25\uff1a\u6307\u51fa\u9884\u671f\u503c\u548c\u51fd\u6570\u8f93\u51fa\u4e0d\u5339\u914d\u7684\u539f\u56e0\u3002 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e2d\u7684\u9519\u8bef\u6d88\u606f\u89e3\u8bfb \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u662f\u4fdd\u8bc1 PHP \u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u5b9e\u8df5\u3002\u9519\u8bef\u6d88\u606f\u5bf9\u4e8e\u7406\u89e3\u6d4b\u8bd5\u5931\u8d25\u7684\u539f\u56e0\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u6b64\u6b63\u786e\u89e3\u8bfb\u8fd9\u4e9b\u6d88\u606f\u5bf9\u4e8e\u8c03\u8bd5\u548c\u4fee\u590d\u81f3\u5173\u91cd\u8981\u3002 1. \u53c2\u6570\u9519\u8bef \u9519\u8bef\u6d88\u606f\u901a\u5e38\u4f1a\u6307\u793a\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u6b63\u786e\u3002\u4f8b\u5982\uff1a Fatal error: Uncaught TypeError: Argument 1 passed to myFunction() must be of the type string, integer given \u6b64\u9519\u8bef\u8868\u793a myFunction() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u6574\u6570\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4fee\u6539\u8c03\u7528\u4ee3\u7801\u4ee5\u63d0\u4f9b\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u3002 2. \u8fd4\u56de\u503c\u9519\u8bef \u9519\u8bef\u6d88\u606f\u4e5f\u53ef\u80fd\u6307\u793a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0d\u5339\u914d\u9884\u671f\u7684\u7c7b\u578b\u6216\u503c\u3002\u4f8b\u5982\uff1a Failed asserting that function &#8220;myFunction&#8221; returns type &#8220;int&#8221;. \u6b64\u9519\u8bef\u8868\u793a myFunction() \u9884\u8ba1\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd4\u56de\u4e86\u4e00\u4e2a\u5176\u4ed6\u7c7b\u578b\u6216\u4e00\u4e2a\u610f\u5916\u7684\u503c\u3002\u68c0\u67e5\u51fd\u6570\u5b9a\u4e49\u4ee5\u786e\u4fdd\u5b83\u8fd4\u56de\u9884\u671f\u7c7b\u578b\u548c\u503c\u3002 3. \u5f02\u5e38 [&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-48169","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48169","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=48169"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48169\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}