{"id":48374,"date":"2024-12-02T17:43:49","date_gmt":"2024-12-02T09:43:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48374\/"},"modified":"2024-12-02T17:43:49","modified_gmt":"2024-12-02T09:43:49","slug":"php%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9aphp%e5%8d%8f%e7%a8%8b%e7%bc%96%e7%a8%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48374\/","title":{"rendered":"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p>\u7b54\u6848\uff1a\u5728 PHP \u4e2d\uff0c\u534f\u7a0b\u901a\u8fc7 generator \u5173\u952e\u5b57\u5b9e\u73b0\u3002\u521b\u5efa\u534f\u7a0b\uff1a\u901a\u8fc7 generator \u751f\u6210\u5668\u521b\u5efa\u534f\u7a0b\u3002\u8c03\u5ea6\u534f\u7a0b\uff1a\u4f7f\u7528 Coroutine \u7c7b\u8c03\u5ea6\u534f\u7a0b\u3002\u4f20\u9012\u53c2\u6570\uff1a\u5728\u521b\u5efa\u534f\u7a0b\u65f6\u4f20\u9012\u53c2\u6570\u3002\u8fd4\u56de\u503c\uff1a\u4f7f\u7528 yield \u8bed\u53e5\u4ece\u534f\u7a0b\u8fd4\u56de\u3002\u534f\u7a0b\u63a7\u5236\uff1a\u4f7f\u7528 suspend()\u548cresume()\u65b9\u6cd5\u6682\u505c\u548c\u6062\u590d\u534f\u7a0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730019326671dfffe9f2a6.jpg\" class=\"aligncenter\" title=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3\u63d2\u56fe\" alt=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<h2>PHP\u534f\u7a0b\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3<\/h2>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u800c\u65e0\u9700\u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u3002\u5728PHP\u4e2d\uff0c\u534f\u7a0b\u901a\u8fc7<code>generator<\/code>\u5173\u952e\u5b57\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u534f\u7a0b\u521b\u5efa<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7<code>generator<\/code>\u751f\u6210\u5668\u521b\u5efa\u534f\u7a0b\uff1a<\/p>\n<pre>function myCoroutine() {\n  \/\/ \u534f\u7a0b\u7684\u4ee3\u7801\n  yield $value;\n}\n\n$coroutine = myCoroutine();<\/pre>\n<p><strong>\u534f\u7a0b\u8c03\u5ea6<\/strong><\/p>\n<p>\u8981\u8c03\u5ea6\u534f\u7a0b\uff0c\u9700\u8981\u4f7f\u7528<code>Coroutine<\/code>\u7c7b\uff1a<\/p>\n<pre>$coroutine = new Coroutine(myCoroutine());\n$coroutine-&gt;run();<\/pre>\n<p><strong>\u4f20\u9012\u53c2\u6570<\/strong><\/p>\n<p>\u53ef\u4ee5\u5728\u521b\u5efa\u534f\u7a0b\u65f6\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<pre>function myCoroutine($param) {\n  \/\/ \u4f7f\u7528 $param \u7684\u4ee3\u7801\n}\n\n$coroutine = new Coroutine(myCoroutine('foo'));\n$coroutine-&gt;run();<\/pre>\n<p><strong>\u534f\u7a0b\u8fd4\u56de\u503c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u4ece\u534f\u7a0b\u8fd4\u56de\uff1a<\/p>\n<pre>function myCoroutine() {\n  yield $value;\n  \/\/ \u66f4\u591a\u7684\u4ee3\u7801\n}\n\n$value = $coroutine-&gt;run();<\/pre>\n<p><strong>\u534f\u7a0b\u63a7\u5236<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>suspend()<\/code>\u548c<code>resume()<\/code>\u65b9\u6cd5\u6682\u505c\u548c\u6062\u590d\u534f\u7a0b\uff1a<\/p>\n<pre>$coroutine-&gt;suspend();\n\/\/ \u5176\u4ed6\u4ee3\u7801\n$coroutine-&gt;resume();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5f02\u6b65HTTP\u5ba2\u6237\u7aef<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528GuzzleHTTP \u5ba2\u6237\u7aef\u8fdb\u884c\u5f02\u6b65HTTP\u8bf7\u6c42\u7684\u793a\u4f8b\u534f\u7a0b\uff1a<\/p>\n<pre>use GuzzleHttp\\Client;\n\nfunction asyncHttpClient() {\n  $client = new Client();\n  while (true) {\n    $query = yield;\n    $response = $client-&gt;request('GET', 'https:\/\/example.com', ['query' =&gt; $query]);\n    yield $response-&gt;getBody()-&gt;getContents();\n  }\n}\n\n$coroutine = new Coroutine(asyncHttpClient());\n$coroutine-&gt;run('foo');<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u534f\u7a0b\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u3002\u5728PHP\u4e2d\u4f7f\u7528\u534f\u7a0b\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u3001\u8c03\u5ea6\u548c\u63a7\u5236\u534f\u7a0b\uff0c\u4ee5\u5b9e\u73b0\u5f02\u6b65\u548c\u9ad8\u5e76\u53d1\u7f16\u7a0b\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u534f\u7a0b\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\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3 \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 \u7b54\u6848\uff1a\u5728 PHP \u4e2d\uff0c\u534f\u7a0b\u901a\u8fc7 generator \u5173\u952e\u5b57\u5b9e\u73b0\u3002\u521b\u5efa\u534f\u7a0b\uff1a\u901a\u8fc7 generator \u751f\u6210\u5668\u521b\u5efa\u534f\u7a0b\u3002\u8c03\u5ea6\u534f\u7a0b\uff1a\u4f7f\u7528 Coroutine \u7c7b\u8c03\u5ea6\u534f\u7a0b\u3002\u4f20\u9012\u53c2\u6570\uff1a\u5728\u521b\u5efa\u534f\u7a0b\u65f6\u4f20\u9012\u53c2\u6570\u3002\u8fd4\u56de\u503c\uff1a\u4f7f\u7528 yield \u8bed\u53e5\u4ece\u534f\u7a0b\u8fd4\u56de\u3002\u534f\u7a0b\u63a7\u5236\uff1a\u4f7f\u7528 suspend()\u548cresume()\u65b9\u6cd5\u6682\u505c\u548c\u6062\u590d\u534f\u7a0b\u3002 PHP\u534f\u7a0b\u7f16\u7a0b\u6307\u5357\uff1aPHP\u534f\u7a0b\u7f16\u7a0b\u8be6\u89e3 \u5f15\u8a00 \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u800c\u65e0\u9700\u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u3002\u5728PHP\u4e2d\uff0c\u534f\u7a0b\u901a\u8fc7generator\u5173\u952e\u5b57\u5b9e\u73b0\u3002 \u534f\u7a0b\u521b\u5efa \u53ef\u4ee5\u901a\u8fc7generator\u751f\u6210\u5668\u521b\u5efa\u534f\u7a0b\uff1a function myCoroutine() { \/\/ \u534f\u7a0b\u7684\u4ee3\u7801 yield $value; } $coroutine = myCoroutine(); \u534f\u7a0b\u8c03\u5ea6 \u8981\u8c03\u5ea6\u534f\u7a0b\uff0c\u9700\u8981\u4f7f\u7528Coroutine\u7c7b\uff1a $coroutine = new Coroutine(myCoroutine()); $coroutine-&gt;run(); \u4f20\u9012\u53c2\u6570 \u53ef\u4ee5\u5728\u521b\u5efa\u534f\u7a0b\u65f6\u4f20\u9012\u53c2\u6570\uff1a function myCoroutine($param) { \/\/ \u4f7f\u7528 $param \u7684\u4ee3\u7801 } $coroutine = new Coroutine(myCoroutine(&#8216;foo&#8217;)); $coroutine-&gt;run(); \u534f\u7a0b\u8fd4\u56de\u503c \u53ef\u4ee5\u4f7f\u7528yield\u8bed\u53e5\u4ece\u534f\u7a0b\u8fd4\u56de\uff1a function [&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-48374","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48374","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=48374"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48374\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}