{"id":202441,"date":"2025-05-13T12:21:15","date_gmt":"2025-05-13T04:21:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202441\/"},"modified":"2025-05-13T12:21:15","modified_gmt":"2025-05-13T04:21:15","slug":"php-%e9%97%ad%e5%8c%85%e7%9a%84%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202441\/","title":{"rendered":"php \u95ed\u5305\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php \u95ed\u5305\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aphp \u95ed\u5305\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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>\u4ee5\u4e0b\u662f\u4f18\u5316 PHP \u95ed\u5305\u6027\u80fd\u7684\u6280\u5de7\uff1a\u4f7f\u7528\u53d8\u91cf\u5f15\u7528\uff1a\u5f15\u7528\u5916\u90e8\u53d8\u91cf\uff0c\u907f\u514d\u521b\u5efa\u526f\u672c\u3002\u5ef6\u8fdf\u95ed\u5305\u521b\u5efa\uff1a\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5ef6\u8fdf\u521b\u5efa\uff0c\u51cf\u5c11\u5f00\u9500\u3002\u4f7f\u7528\u6301\u4e45\u95ed\u5305\uff1a\u4ec5\u521b\u5efa\u4e00\u6b21\u95ed\u5305\uff0c\u51cf\u5c11\u521b\u5efa\u5f00\u9500\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241016\/1729082320670fb3d003174.jpg\" class=\"aligncenter\"><\/p>\n<h2>PHP \u95ed\u5305\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h2>\n<p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684PHP\u51fd\u6570\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u51fd\u6570\u4e4b\u5916\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u7136\u800c\uff0c\u9891\u7e41\u5730\u521b\u5efa\u95ed\u5305\u53ef\u80fd\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u8d1f\u9762\u5f71\u54cd\u3002\u4e3a\u4e86\u4f18\u5316\u95ed\u5305\u7684\u6027\u80fd\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b\u6280\u5de7\uff1a<\/p>\n<h3>\u4f7f\u7528\u53d8\u91cf\u5f15\u7528<\/h3>\n<p>\u95ed\u5305\u5185\u90e8\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u8fd9\u4e9b\u53d8\u91cf\u7684\u526f\u672c\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u4e0d\u5fc5\u8981\u7684\u590d\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u5f15\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$x = 10;\n\n$closure = function() use (&amp;$x) {\n    echo $x;\n};<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 <code>use (&amp;$x)<\/code>\uff0c\u95ed\u5305\u5c06\u5f15\u7528\u5916\u90e8\u53d8\u91cf <code>$x<\/code>\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u5b83\u7684\u526f\u672c\u3002<\/p>\n<h3>\u5ef6\u8fdf\u95ed\u5305\u7684\u521b\u5efa<\/h3>\n<p>\u5728\u521b\u5efa\u95ed\u5305\u65f6\uff0c\u4f1a\u5206\u914d\u5185\u5b58\u5e76\u751f\u6210\u5b57\u8282\u7801\u3002\u4e3a\u4e86\u51cf\u5c11\u521b\u5efa\u95ed\u5305\u7684\u5f00\u9500\uff0c\u53ef\u4ee5\u5ef6\u8fdf\u95ed\u5305\u7684\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5ef6\u8fdf\u521b\u5efa\u95ed\u5305\uff1a<\/p>\n<pre>$callback = function($x) {\n    return $x * $x;\n};\n\n\/\/ \u5f53\u9700\u8981\u65f6\u518d\u8c03\u7528\u56de\u8c03\u51fd\u6570\n$result = $callback(10);<\/pre>\n<h3>\u4f7f\u7528\u6301\u4e45\u95ed\u5305<\/h3>\n<p>\u5f53\u95ed\u5305\u4e0d\u9700\u8981\u5728\u591a\u4e2a\u8bf7\u6c42\u4e2d\u5171\u4eab\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u6301\u4e45\u95ed\u5305\u3002\u6301\u4e45\u95ed\u5305\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21\uff0c\u5e76\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u51cf\u5c11\u4e86\u521b\u5efa\u5f00\u9500\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u6301\u4e45\u95ed\u5305\n$persistentClosure = function($x) {\n    return $x * $x;\n};\n\n\/\/ \u591a\u6b21\u8c03\u7528\u6301\u4e45\u95ed\u5305\nfor ($i = 0; $i &lt; 100000; $i++) {\n    $result = $persistentClosure($i);\n}<\/pre>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f18\u5316\u95ed\u5305\u6027\u80fd\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>\/\/ \u672a\u7ecf\u4f18\u5316\u7684\u95ed\u5305\nfunction getSum($x) {\n    $y = 10;\n    return function() use ($x, $y) {\n        return $x + $y;\n    };\n}\n\n\/\/ \u4f7f\u7528\u5f15\u7528\u4f18\u5316\u7684\u95ed\u5305\nfunction getSumOptimized($x) {\n    $y = 10;\n    return function() use (&amp;$x, &amp;$y) {\n        return $x + $y;\n    };\n}\n\n$closure1 = getSum(1);\n$closure2 = getSumOptimized(1);\n\n\/\/ \u5bf9\u95ed\u5305\u8fdb\u884c\u591a\u6b21\u8c03\u7528\nfor ($i = 0; $i &lt; 1000000; $i++) {\n    $closure1();\n    $closure2();\n}\n\n\/\/ \u67e5\u770b\u4f18\u5316\u7684\u95ed\u5305\u660e\u663e\u66f4\u5feb<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php \u95ed\u5305\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7 \u5728I&#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-202441","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202441","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=202441"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202441\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}