{"id":202420,"date":"2025-05-13T09:48:19","date_gmt":"2025-05-13T01:48:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202420\/"},"modified":"2025-05-13T09:48:19","modified_gmt":"2025-05-13T01:48:19","slug":"php-%e5%87%bd%e6%95%b0%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%ba%94%e7%94%a8%e4%b8%8e%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202420\/","title":{"rendered":"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b\u8bd5<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b\u8bd5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241013\/1728782513670b20b19cd2f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><br \/>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u5728 PHP \u4e2d\u7ec4\u7ec7\u548c\u91cd\u7528\u4ee3\u7801\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u5177\u53ef\u6269\u5c55\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u7684\u4ee3\u7801\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4e24\u79cd\u5e38\u7528\u7684\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u4f7f\u7528 PHPUnit \u9a8c\u8bc1\u5b83\u4eec\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p><strong>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/p>\n<p><strong>1. \u7b56\u7565\u6a21\u5f0f<\/strong><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u5141\u8bb8\u6211\u4eec\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u4f7f\u7528\u4e0d\u540c\u7684\u7b97\u6cd5\u3002\u5b83\u5c06\u7b97\u6cd5\u5c01\u88c5\u5728\u4e0d\u540c\u7684\u7b56\u7565\u7c7b\u4e2d\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u62bd\u8c61\u7684\u63a5\u53e3\u6765\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u62bd\u8c61\u7b56\u7565\u63a5\u53e3\ninterface SortStrategy\n{\n    public function sort(array $list): array;\n}\n\n\/\/ \u5177\u4f53\u7b56\u7565\u7c7b\uff1a\u5192\u6ce1\u6392\u5e8f\nclass BubbleSortStrategy implements SortStrategy\n{\n    public function sort(array $list): array\n    {\n        ...\n    }\n}\n\n\/\/ \u5177\u4f53\u7b56\u7565\u7c7b\uff1a\u63d2\u5165\u6392\u5e8f\nclass InsertSortStrategy implements SortStrategy\n{\n    public function sort(array $list): array\n    {\n        ...\n    }\n}\n\n\/\/ \u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\n$list = [1, 5, 2, 3, 4];\n$strategy = new BubbleSortStrategy();\n$sortedList = $strategy-&gt;sort($list);<\/pre>\n<p><strong>2. \u547d\u4ee4\u6a21\u5f0f<\/strong><\/p>\n<p>\u547d\u4ee4\u6a21\u5f0f\u5141\u8bb8\u6211\u4eec\u5c06\u8bf7\u6c42\u5c01\u88c5\u5728\u5bf9\u8c61\u4e2d\uff0c\u4ece\u800c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bb0\u5f55\u3001\u64a4\u6d88\u548c\u91cd\u505a\u64cd\u4f5c\u3002<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u62bd\u8c61\u547d\u4ee4\u63a5\u53e3\ninterface Command\n{\n    public function execute();\n}\n\n\/\/ \u5177\u4f53\u547d\u4ee4\uff1a\u6dfb\u52a0\u5546\u54c1\u5230\u8d2d\u7269\u8f66\nclass AddItemToCartCommand implements Command\n{\n    public function execute()\n    {\n        \/\/ \u6dfb\u52a0\u5546\u54c1\u5230\u8d2d\u7269\u8f66\n    }\n}\n\n\/\/ \u5177\u4f53\u547d\u4ee4\uff1a\u5220\u9664\u5546\u54c1\u4ece\u8d2d\u7269\u8f66\nclass RemoveItemFromCartCommand implements Command\n{\n    public function execute()\n    {\n        \/\/ \u5220\u9664\u5546\u54c1\u4ece\u8d2d\u7269\u8f66\n    }\n}\n\n\/\/ \u4f7f\u7528\u547d\u4ee4\u6a21\u5f0f\n$commands = [\n    new AddItemToCartCommand(),\n    new RemoveItemFromCartCommand(),\n];\n\nforeach ($commands as $command) {\n    $command-&gt;execute();\n}<\/pre>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u9a8c\u8bc1\u4ee3\u7801\u6b63\u786e\u6027\u7684\u91cd\u8981\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u6765\u6d4b\u8bd5\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u6d4b\u8bd5\u7b56\u7565\u6a21\u5f0f<\/strong><\/p>\n<pre>class BubbleSortStrategyTest extends PHPUnit_Framework_TestCase\n{\n    public function testSort()\n    {\n        $strategy = new BubbleSortStrategy();\n        $list = [1, 5, 2, 3, 4];\n        $sortedList = $strategy-&gt;sort($list);\n\n        $expectedResult = [1, 2, 3, 4, 5];\n        $this-&gt;assertEquals($expectedResult, $sortedList);\n    }\n}<\/pre>\n<p><strong>\u6d4b\u8bd5\u547d\u4ee4\u6a21\u5f0f<\/strong><\/p>\n<pre>class AddItemToCartCommandTest extends PHPUnit_Framework_TestCase\n{\n    public function testExecute()\n    {\n        $command = new AddItemToCartCommand();\n        $command-&gt;execute();\n\n        \/\/ \u65ad\u8a00\u8d2d\u7269\u8f66\u4e2d\u5df2\u6dfb\u52a0\u8be5\u5546\u54c1\n    }\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><br \/>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u53ef\u4ee5\u663e\u8457\u589e\u5f3a PHP \u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u9ad8\u8d28\u91cf\u548c\u66f4\u53ef\u9760\u7684\u8f6f\u4ef6\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b\u8bd5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u5355\u5143\u6d4b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202420","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202420"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202420\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}