{"id":47888,"date":"2024-12-02T11:22:10","date_gmt":"2024-12-02T03:22:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47888\/"},"modified":"2024-12-02T11:22:10","modified_gmt":"2024-12-02T03:22:10","slug":"php-%e5%87%bd%e6%95%b0%e7%9a%84%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e5%a6%82%e4%bd%95%e4%b8%8e%e9%97%ad%e5%8c%85%e9%85%8d%e5%90%88%e4%bd%bf%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47888\/","title":{"rendered":"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u4e0e\u95ed\u5305\u7ed3\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u53ef\u52a8\u6001\u66f4\u65b0\u7684\u3001\u53ef\u91cd\u7528\u7684\u56de\u8c03\u3002\u7ed3\u5408\u65b9\u5f0f\u662f\u5c06\u51fd\u6570\u6307\u9488\u7528\u5728\u95ed\u5305\u4e2d\uff0c\u5e76\u5229\u7528\u95ed\u5305\u7684 use \u5173\u952e\u5b57\u6355\u83b7\u6240\u9700\u7684\u5916\u90e8\u53d8\u91cf\u3002\u8fd9\u79cd\u7ed3\u5408\u7684\u4f18\u70b9\u5305\u62ec\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u4f18\u5316\u53ef\u7ef4\u62a4\u6027\u4ee5\u53ca\u589e\u5f3a\u7075\u6d3b\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729941523671cd0135a533.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7684\u7ed3\u5408<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5b58\u50a8\u51fd\u6570\u5730\u5740\u7684\u53ef\u8c03\u7528\u53d8\u91cf\u3002\u95ed\u5305\u662f\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5176\u521b\u5efa\u65f6\u7684\u5916\u90e8\u53d8\u91cf\u3002\u5c06\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7ed3\u5408\u8d77\u6765\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ef\u91cd\u7528\u7684\u56de\u8c03\uff0c\u8be5\u56de\u8c03\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u5730\u52a8\u6001\u66f4\u65b0\u3002<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fn<\/code> \u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>$myFunctionPointer = fn() =&gt; echo 'Hello World!';<\/pre>\n<p>\u8981\u5c06\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7ed3\u5408\u8d77\u6765\uff0c\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u4f7f\u7528 <code>use<\/code> \u5173\u952e\u5b57\u6355\u83b7\u6240\u9700\u7684\u5916\u90e8\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>$counter = 0;\n\n$callback = fn(string $name) use (&amp;$counter) =&gt; {\n    echo \"Hello $name! Counter: $counter\\n\";\n    $counter++;\n};<\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u5305\u542b\u51fd\u6570\u6307\u9488 <code>fn()<\/code> \u548c\u5916\u90e8\u53d8\u91cf\u5f15\u7528 <code>&amp;$counter<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u9700\u8981\u5bf9\u6bcf\u4e2a\u7528\u6237\u6267\u884c\u4e0d\u540c\u64cd\u4f5c\u7684\u5e94\u7528\u573a\u666f\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6765\u5b58\u50a8\u8981\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u5e76\u5728\u95ed\u5305\u4e2d\u6355\u83b7\u7528\u6237\u7279\u5b9a\u7684\u53d8\u91cf\uff1a<\/p>\n<pre>$users = ['John', 'Jane', 'Bob'];\n\n$operations = [\n    'John' =&gt; fn() =&gt; echo 'Send email to John',\n    'Jane' =&gt; fn() =&gt; echo 'Show chat widget to Jane',\n    'Bob' =&gt; fn() =&gt; echo 'Display notifications to Bob',\n];\n\nforeach ($users as $user) {\n    call_user_func($operations[$user], $user);\n}<\/pre>\n<p>\u8fd9\u5c06\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u7528\u6237\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u4f7f\u7528\u51fd\u6570\u6307\u9488\u548c\u95ed\u5305\u7ed3\u5408\u7684\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027<\/li>\n<li>\u4f18\u5316\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u589e\u5f3a\u4ee3\u7801\u7684\u7075\u6d3b\u6027<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\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 \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u4e0e\u95ed\u5305\u914d\u5408\u4f7f\u7528\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u51fd\u6570\u6307\u9488\u53ef\u4ee5\u4e0e\u95ed\u5305\u7ed3\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u53ef\u52a8\u6001\u66f4\u65b0\u7684\u3001\u53ef\u91cd\u7528\u7684\u56de\u8c03\u3002\u7ed3\u5408\u65b9\u5f0f\u662f\u5c06\u51fd\u6570\u6307\u9488\u7528\u5728\u95ed\u5305\u4e2d\uff0c\u5e76\u5229\u7528\u95ed\u5305\u7684 use \u5173\u952e\u5b57\u6355\u83b7\u6240\u9700\u7684\u5916\u90e8\u53d8\u91cf\u3002\u8fd9\u79cd\u7ed3\u5408\u7684\u4f18\u70b9\u5305\u62ec\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u4f18\u5316\u53ef\u7ef4\u62a4\u6027\u4ee5\u53ca\u589e\u5f3a\u7075\u6d3b\u6027\u3002 PHP \u4e2d\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7684\u7ed3\u5408 \u5728 PHP \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5b58\u50a8\u51fd\u6570\u5730\u5740\u7684\u53ef\u8c03\u7528\u53d8\u91cf\u3002\u95ed\u5305\u662f\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5176\u521b\u5efa\u65f6\u7684\u5916\u90e8\u53d8\u91cf\u3002\u5c06\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7ed3\u5408\u8d77\u6765\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ef\u91cd\u7528\u7684\u56de\u8c03\uff0c\u8be5\u56de\u8c03\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u5730\u52a8\u6001\u66f4\u65b0\u3002 \u7528\u6cd5 \u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u53ef\u4ee5\u4f7f\u7528 fn \u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a $myFunctionPointer = fn() =&gt; echo &#8216;Hello World!&#8217;; \u8981\u5c06\u51fd\u6570\u6307\u9488\u4e0e\u95ed\u5305\u7ed3\u5408\u8d77\u6765\uff0c\u53ef\u4ee5\u5728\u95ed\u5305\u4e2d\u4f7f\u7528 use \u5173\u952e\u5b57\u6355\u83b7\u6240\u9700\u7684\u5916\u90e8\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a $counter = 0; $callback = fn(string $name) use (&amp;$counter) =&gt; { echo &#8220;Hello $name! Counter: $counter\\n&#8221;; $counter++; }; \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u5305\u542b\u51fd\u6570\u6307\u9488 fn() \u548c\u5916\u90e8\u53d8\u91cf\u5f15\u7528 &amp;$counter\u3002 \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4e00\u4e2a\u9700\u8981\u5bf9\u6bcf\u4e2a\u7528\u6237\u6267\u884c\u4e0d\u540c\u64cd\u4f5c\u7684\u5e94\u7528\u573a\u666f\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6765\u5b58\u50a8\u8981\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u5e76\u5728\u95ed\u5305\u4e2d\u6355\u83b7\u7528\u6237\u7279\u5b9a\u7684\u53d8\u91cf\uff1a $users = [&#8216;John&#8217;, &#8216;Jane&#8217;, &#8216;Bob&#8217;]; [&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-47888","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47888","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=47888"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47888\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}