{"id":47072,"date":"2024-12-02T10:22:28","date_gmt":"2024-12-02T02:22:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47072\/"},"modified":"2024-12-02T10:22:28","modified_gmt":"2024-12-02T02:22:28","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e8%87%aa%e5%8f%8d%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47072\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570<\/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 \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\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>PHP \u4e2d\u53ef\u4f7f\u7528 \\Closure::fromCallable() \u51fd\u6570\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u3002\u81ea\u53cd\u51fd\u6570\u662f\u6307\u6307\u5411\u81ea\u8eab\u6216\u540c\u4e00\u7c7b\u7684\u51fd\u6570\u3002\u8bed\u6cd5\u4e3a\uff1a\\Closure::fromCallable($callable)\uff0c\u5176\u4e2d $callable \u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 PHP \u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f8b\u5982\u51fd\u6570\u540d\u3001\u65b9\u6cd5\u540d\u6216\u533f\u540d\u51fd\u6570\u3002\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u65f6\u9700\u786e\u4fdd\u53ef\u8c03\u7528\u5bf9\u8c61\u5728 Closure \u5b9e\u4f8b\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u4fdd\u6301\u6709\u6548\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729864289671ba2616d270.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8fd4\u56de\u81ea\u53cd\u51fd\u6570<\/strong><\/p>\n<p>\u81ea\u53cd\u51fd\u6570\u662f\u6307\u6307\u5411\u81ea\u8eab\u6216\u540c\u4e00\u7c7b\u7684\u51fd\u6570\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\\Closure::fromCallable()<\/code> \u65b9\u6cd5\u6765\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>\\Closure::fromCallable($callable);<\/pre>\n<p>\u5176\u4e2d <code>$callable<\/code> \u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 PHP \u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f8b\u5982\u51fd\u6570\u540d\u3001\u65b9\u6cd5\u540d\u6216\u533f\u540d\u51fd\u6570\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>increment<\/code> \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5176\u589e\u91cf\u503c\uff1a<\/p>\n<pre>function increment($number) {\n    return $number + 1;\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>\\Closure::fromCallable()<\/code> \u65b9\u6cd5\u5c06\u6b64\u51fd\u6570\u8f6c\u6362\u4e3a\u81ea\u53cd\u51fd\u6570\uff1a<\/p>\n<pre>$incrementSelf = \\Closure::fromCallable('increment');<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 <code>$incrementSelf<\/code> \u81ea\u53cd\u51fd\u6570\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff1a<\/p>\n<pre>function applyIncrement($number, callable $increment) {\n    return $increment($number);\n}\n\n$result = applyIncrement(5, $incrementSelf);\n\necho $result; \/\/ \u8f93\u51fa\uff1a6<\/pre>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u65f6\uff0c\u8bf7\u786e\u4fdd\u53ef\u8c03\u7528\u5bf9\u8c61\u5728 Closure \u5b9e\u4f8b\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u4fdd\u6301\u6709\u6548\u3002<\/li>\n<li>\u81ea\u53cd\u51fd\u6570\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4f46\u4f7f\u7528\u65f6\u5e94\u8c28\u614e\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5faa\u73af\u5f15\u7528\u548c\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570 \u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\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 PHP \u4e2d\u53ef\u4f7f\u7528 \\Closure::fromCallable() \u51fd\u6570\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u3002\u81ea\u53cd\u51fd\u6570\u662f\u6307\u6307\u5411\u81ea\u8eab\u6216\u540c\u4e00\u7c7b\u7684\u51fd\u6570\u3002\u8bed\u6cd5\u4e3a\uff1a\\Closure::fromCallable($callable)\uff0c\u5176\u4e2d $callable \u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 PHP \u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f8b\u5982\u51fd\u6570\u540d\u3001\u65b9\u6cd5\u540d\u6216\u533f\u540d\u51fd\u6570\u3002\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u65f6\u9700\u786e\u4fdd\u53ef\u8c03\u7528\u5bf9\u8c61\u5728 Closure \u5b9e\u4f8b\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u4fdd\u6301\u6709\u6548\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8fd4\u56de\u81ea\u53cd\u51fd\u6570 \u81ea\u53cd\u51fd\u6570\u662f\u6307\u6307\u5411\u81ea\u8eab\u6216\u540c\u4e00\u7c7b\u7684\u51fd\u6570\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 \\Closure::fromCallable() \u65b9\u6cd5\u6765\u8fd4\u56de\u81ea\u53cd\u51fd\u6570\u3002 \u8bed\u6cd5 \\Closure::fromCallable($callable); \u5176\u4e2d $callable \u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 PHP \u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f8b\u5982\u51fd\u6570\u540d\u3001\u65b9\u6cd5\u540d\u6216\u533f\u540d\u51fd\u6570\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a increment \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5176\u589e\u91cf\u503c\uff1a function increment($number) { return $number + 1; } \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \\Closure::fromCallable() \u65b9\u6cd5\u5c06\u6b64\u51fd\u6570\u8f6c\u6362\u4e3a\u81ea\u53cd\u51fd\u6570\uff1a $incrementSelf = \\Closure::fromCallable(&#8216;increment&#8217;); \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 $incrementSelf \u81ea\u53cd\u51fd\u6570\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff1a function applyIncrement($number, callable $increment) { [&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-47072","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47072","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=47072"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47072\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}