{"id":48460,"date":"2024-12-02T17:11:36","date_gmt":"2024-12-02T09:11:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48460\/"},"modified":"2024-12-02T17:11:36","modified_gmt":"2024-12-02T09:11:36","slug":"php-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%e4%b8%8e%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48460\/","title":{"rendered":"PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u7ec4\u7ec7 PHP \u7c7b\uff0c\u9632\u6b62\u540d\u79f0\u51b2\u7a81\u3002\u5355\u5143\u6d4b\u8bd5\u53ef\u9a8c\u8bc1\u4ee3\u7801\u529f\u80fd\uff0c\u4f7f\u7528 PHPUnit \u53ef\u7f16\u5199\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa\u9879\u76ee\u7ed3\u6784\uff0c\u5b9a\u4e49 Myclass \u7c7b\uff0c\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\uff0c\u4f7f\u7528 PHPUnit \u8fd0\u884c\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730032642671e34021265b.jpg\" class=\"aligncenter\" title=\"PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f\u63d2\u56fe\" alt=\"PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p><strong>\u547d\u540d\u7a7a\u95f4<\/strong><\/p>\n<p>\u547d\u540d\u7a7a\u95f4\u662f PHP \u4e2d\u7528\u4e8e\u7ec4\u7ec7\u548c\u547d\u540d\u7c7b\u7684\u91cd\u8981\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u60a8\u5c06\u76f8\u5173\u7c7b\u5206\u7ec4\u5230\u4e00\u4e2a\u903b\u8f91\u5355\u5143\u4e2d\uff0c\u5e76\u9632\u6b62\u540d\u79f0\u51b2\u7a81\u3002<\/p>\n<p><strong>\u7533\u660e\u547d\u540d\u7a7a\u95f4<\/strong><\/p>\n<p>\u8981\u7533\u660e\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u8bf7\u4f7f\u7528 <code>namespace<\/code> \u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u547d\u540d\u7a7a\u95f4\u7684\u540d\u79f0\uff1a<\/p>\n<pre>namespace MyNamespace;\n\nclass Myclass {}<\/pre>\n<p><strong>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7c7b<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7c7b\uff0c\u8bf7\u5728\u5176\u540d\u79f0\u524d\u52a0\u4e0a\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<pre>MyNamespace\\Myclass::myMethod();<\/pre>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u9a8c\u8bc1\u4ee3\u7801\u529f\u80fd\u7684\u7edd\u4f73\u65b9\u6cd5\u3002\u4f7f\u7528 PHPUnit \u7b49\u6846\u67b6\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u7f16\u5199\u548c\u8fd0\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002<\/p>\n<p><strong>\u64b0\u5199\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u7684 <code>TestCase<\/code> \u7c7b\u5e76\u7533\u660e\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass MyTest extends TestCase {\n    public function testMethod() {\n        \/\/ \u6d4b\u8bd5\u4ee3\u7801\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u521b\u5efa\u9879\u76ee\u7ed3\u6784<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<pre>\u251c\u2500\u2500 myapp\n    \u251c\u2500\u2500 src\n    \u2502&nbsp;&nbsp; \u251c\u2500\u2500 MyNamespace\n    \u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2514\u2500\u2500 Myclass.php\n    \u251c\u2500\u2500 tests\n    \u2502&nbsp;&nbsp; \u2514\u2500\u2500 MyTest.php<\/pre>\n<p><strong>\u7f16\u5199\u4ee3\u7801<\/strong><\/p>\n<p>\u5728 <code>Myclass.php<\/code> \u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u6211\u4eec\u7684\u7c7b\uff1a<\/p>\n<pre>namespace MyNamespace;\n\nclass Myclass {\n    public function myMethod() {\n        return 'Hello World!';\n    }\n}<\/pre>\n<p><strong>\u7f16\u5199\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5728 <code>MyTest.php<\/code> \u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\nuse MyNamespace\\Myclass;\n\nclass MyTest extends TestCase {\n    public function testMethod() {\n        \/\/ \u521b\u5efa\u4e00\u4e2a Myclass \u5b9e\u4f8b\n        $myclass = new Myclass();\n\n        \/\/ \u65ad\u8a00 myMethod() \u8fd4\u56de\u9884\u671f\u7684\u503c\n        $this-&gt;assertEquals('Hello World!', $myclass-&gt;myMethod());\n    }\n}<\/pre>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/p>\n<p>\u8981\u8fd0\u884c\u6d4b\u8bd5\uff0c\u8bf7\u4f7f\u7528 PHPUnit \u547d\u4ee4\u884c\u5de5\u5177\uff1a<\/p>\n<pre>phpunit tests\/MyTest.php<\/pre>\n<p><strong>\u9a8c\u8bc1\u7ed3\u679c<\/strong><\/p>\n<p>\u5982\u679c\u6d4b\u8bd5\u6210\u529f\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre>PHPUnit 9.5.20 by Sebastian Bergmann and contributors.\n\n.\n\nTime: 10 ms, Memory: 7.00 MB\n\nOK (1 test, 1 assertion)<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f \u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5\uff1f\u300b\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u6216\u8005\u662f\u5bf9\u6587\u7ae0\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01 \u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u7ec4\u7ec7 PHP \u7c7b\uff0c\u9632\u6b62\u540d\u79f0\u51b2\u7a81\u3002\u5355\u5143\u6d4b\u8bd5\u53ef\u9a8c\u8bc1\u4ee3\u7801\u529f\u80fd\uff0c\u4f7f\u7528 PHPUnit \u53ef\u7f16\u5199\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa\u9879\u76ee\u7ed3\u6784\uff0c\u5b9a\u4e49 Myclass \u7c7b\uff0c\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\uff0c\u4f7f\u7528 PHPUnit \u8fd0\u884c\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u7ed3\u679c\u3002 PHP \u547d\u540d\u7a7a\u95f4\u4e0e\u5355\u5143\u6d4b\u8bd5 \u547d\u540d\u7a7a\u95f4 \u547d\u540d\u7a7a\u95f4\u662f PHP \u4e2d\u7528\u4e8e\u7ec4\u7ec7\u548c\u547d\u540d\u7c7b\u7684\u91cd\u8981\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u60a8\u5c06\u76f8\u5173\u7c7b\u5206\u7ec4\u5230\u4e00\u4e2a\u903b\u8f91\u5355\u5143\u4e2d\uff0c\u5e76\u9632\u6b62\u540d\u79f0\u51b2\u7a81\u3002 \u7533\u660e\u547d\u540d\u7a7a\u95f4 \u8981\u7533\u660e\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u8bf7\u4f7f\u7528 namespace \u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u547d\u540d\u7a7a\u95f4\u7684\u540d\u79f0\uff1a namespace MyNamespace; class Myclass {} \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u7c7b \u8981\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7c7b\uff0c\u8bf7\u5728\u5176\u540d\u79f0\u524d\u52a0\u4e0a\u547d\u540d\u7a7a\u95f4\uff1a MyNamespace\\Myclass::myMethod(); \u5355\u5143\u6d4b\u8bd5 \u5355\u5143\u6d4b\u8bd5\u662f\u9a8c\u8bc1\u4ee3\u7801\u529f\u80fd\u7684\u7edd\u4f73\u65b9\u6cd5\u3002\u4f7f\u7528 PHPUnit \u7b49\u6846\u67b6\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u7f16\u5199\u548c\u8fd0\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002 \u64b0\u5199\u5355\u5143\u6d4b\u8bd5 \u4e3a\u4e86\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u7684 TestCase \u7c7b\u5e76\u7533\u660e\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\uff1a use PHPUnit\\Framework\\TestCase; class MyTest extends TestCase { public function testMethod() { \/\/ \u6d4b\u8bd5\u4ee3\u7801 } [&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-48460","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48460","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=48460"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48460\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}