{"id":202402,"date":"2025-05-13T08:18:08","date_gmt":"2025-05-13T00:18:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202402\/"},"modified":"2025-05-13T08:18:08","modified_gmt":"2025-05-13T00:18:08","slug":"php%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%e7%9a%84%e4%bc%98%e5%8a%bf%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202402\/","title":{"rendered":"php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\uff08FOP\uff09\u5728 PHP \u4e2d\u7684\u4e3b\u8981\u4f18\u52bf\u5305\u62ec\uff1a\u4ee3\u7801\u53ef\u91cd\u7528\u6027\uff1a\u5c06\u51fd\u6570\u4f5c\u4e3a\u53d8\u91cf\u4f20\u9012\u548c\u8fd4\u56de\u3002\u6a21\u5757\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff1a\u901a\u8fc7\u5c06\u51fd\u6570\u7ec4\u7ec7\u6210\u72ec\u7acb\u6a21\u5757\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528\u533f\u540d\u51fd\u6570\u7ed3\u5408\u51fd\u6570\u5bf9\u8c61\u8fdb\u884c\u4f18\u5316\u3002\u53ef\u6d4b\u8bd5\u6027\uff1a\u72ec\u7acb\u6267\u884c\u51fd\u6570\u5bf9\u8c61\uff0c\u4fbf\u4e8e\u6d4b\u8bd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241012\/17287006766709e104af195.jpg\" class=\"aligncenter\"><\/p>\n<h2>PHP \u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\uff1a\u4f18\u52bf\u548c\u5b9e\u6218\u6848\u4f8b<\/h2>\n<p><strong>\u524d\u8a00<\/strong><br \/>\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\uff08FOP\uff09\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5c06\u51fd\u6570\u89c6\u4e3a\u4e00\u7b49\u5bf9\u8c61\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u63d0\u4f9b\u4e86\u8bb8\u591a\u4f18\u52bf\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u53ef\u91cd\u7528\u6027<\/li>\n<li>\u6a21\u5757\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u6027\u80fd\u4f18\u5316<\/li>\n<li>\u53ef\u6d4b\u8bd5\u6027<\/li>\n<\/ul>\n<p><strong>PHP \u4e2d\u7684\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b<\/strong><br \/>PHP \u4e2d\u7684\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u662f\u4f7f\u7528 <code>Closure<\/code> \u7c7b\u5b9e\u73b0\u7684\u3002<code>Closure<\/code> \u5141\u8bb8\u60a8\u521b\u5efa\u53ef\u4ee5\u50cf\u5e38\u89c4\u53d8\u91cf\u4e00\u6837\u5728\u4ee3\u7801\u4e2d\u4f20\u9012\u548c\u8fd4\u56de\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u521b\u5efa\u51fd\u6570\u5bf9\u8c61<\/strong><br \/>\u8981\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a <code>Closure<\/code> \u5bf9\u8c61\uff1a<\/p>\n<pre>$closure = function ($param1, $param2) {\n    \/\/ \u51fd\u6570\u4f53\n};<\/pre>\n<p><strong>\u4f7f\u7528\u51fd\u6570\u5bf9\u8c61<\/strong><br \/>\u60a8\u53ef\u4ee5\u50cf\u4f7f\u7528\u5e38\u89c4\u51fd\u6570\u4e00\u6837\u4f7f\u7528\u51fd\u6570\u5bf9\u8c61\uff1a<\/p>\n<pre>$closure(\"arg1\", \"arg2\");<\/pre>\n<p><strong>\u4f18\u52bf<\/strong><br \/><strong>\u4ee3\u7801\u53ef\u91cd\u7528\u6027\uff1a<\/strong>\u51fd\u6570\u5bf9\u8c61\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4ece\u800c\u5141\u8bb8\u4ee3\u7801\u91cd\u7528\u3002<\/p>\n<p><strong>\u6a21\u5757\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u51fd\u6570\u5bf9\u8c61\u53ef\u4ee5\u7ec4\u7ec7\u6210\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u6027\u80fd\u4f18\u5316\uff1a<\/strong>\u51fd\u6570\u5bf9\u8c61\u53ef\u4ee5\u4e0e [\u533f\u540d\u51fd\u6570](https:\/\/www.php.net\/manual\/zh\/functions.anonymous.php) \u7ed3\u5408\u4f7f\u7528\uff0c\u8fd9\u6709\u52a9\u4e8e\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<p><strong>\u53ef\u6d4b\u8bd5\u6027\uff1a<\/strong>\u51fd\u6570\u5bf9\u8c61\u6613\u4e8e\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u72ec\u7acb\u4e8e\u5176\u4ed6\u4ee3\u7801\u6267\u884c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u9ad8\u9636\u51fd\u6570<\/strong><br \/>\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u51fd\u6570\u3002\u51fd\u6570\u5bf9\u8c61\u662f\u5b9e\u73b0\u9ad8\u9636\u51fd\u6570\u7684\u7406\u60f3\u9009\u62e9\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u5b9e\u73b0\u6392\u5e8f\u7b97\u6cd5\u7684 comparator \u51fd\u6570\n$compareFunction = function ($a, $b) {\n    return $a &lt;=&gt; $b;\n};\n\n\/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\n$sortedArray = usort($array, $compareFunction);<\/pre>\n<p><strong>\u56de\u8c03\u51fd\u6570<\/strong><br \/>\u56de\u8c03\u51fd\u6570\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570\u3002\u51fd\u6570\u5bf9\u8c61\u975e\u5e38\u9002\u5408\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528\u56de\u8c03\u51fd\u6570\u8fc7\u6ee4\u6570\u7ec4\n$filteredArray = array_filter($array, function ($element) {\n    return $element &gt; 10;\n});<\/pre>\n<p><strong>\u603b\u7ed3<\/strong><br \/>\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u5728 PHP \u4e2d\u63d0\u4f9b\u4e86\u4f17\u591a\u4f18\u52bf\uff0c\u5305\u62ec\u4ee3\u7801\u53ef\u91cd\u7528\u6027\u3001\u6a21\u5757\u6027\u3001\u6027\u80fd\u4f18\u5316\u548c\u53ef\u6d4b\u8bd5\u6027\u3002\u901a\u8fc7\u4f7f\u7528 <code>Closure<\/code> \u7c7b\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u4f7f\u7528\u51fd\u6570\u5bf9\u8c61\u6765\u589e\u5f3a\u60a8\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u5bf9\u8c61\u7f16\u7a0b\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aphp\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u7684\u4f18\u52bf\u662f\u4ec0&#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-202402","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202402","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=202402"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202402\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}