{"id":46706,"date":"2024-12-02T08:25:19","date_gmt":"2024-12-02T00:25:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46706\/"},"modified":"2024-12-02T08:25:19","modified_gmt":"2024-12-02T00:25:19","slug":"%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%88%9d%e6%8e%a2%ef%bc%9aphp%e5%87%bd%e6%95%b0%e7%9a%84%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46706\/","title":{"rendered":"\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729840964671b47444a99c.jpg\" class=\"aligncenter\" title=\"\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3\u63d2\u56fe\" alt=\"\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3<\/strong><\/p>\n<p>PHP\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u63d0\u5347\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPHP \u4e2d\u7528\u4e8e\u5e76\u53d1\u7f16\u7a0b\u7684\u5173\u952e\u51fd\u6570\u3002<\/p>\n<p><strong>\u534f\u7a0b<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u4ee3\u7801\u3002PHP 8.1\u5f15\u5165\u4e86\u534f\u7a0b\uff0c\u4f7f\u7528<code>\\Generator<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\uff1a<\/p>\n<pre>function myCoroutine() {\n    yield \"Hello\";\n}\n\n$coroutine = myCoroutine();\necho $coroutine-&gt;current(); \/\/ Output: Hello<\/pre>\n<p><strong>\u5e76\u53d1\u51fd\u6570<\/strong><\/p>\n<p>PHP\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5e76\u53d1\u51fd\u6570\uff1a<\/p>\n<ul>\n<li><strong>coroutine_create():<\/strong> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\u3002<\/li>\n<li><strong>coroutine_resume():<\/strong> \u6062\u590d\u4e00\u4e2a\u6302\u8d77\u7684\u534f\u7a0b\u3002<\/li>\n<li><strong>coroutine_yield():<\/strong> \u6302\u8d77\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002<\/li>\n<li><strong>coroutine_close():<\/strong> \u9500\u6bc1\u4e00\u4e2a\u534f\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u6f14\u793a\u4e86\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5e76\u53d1\u6587\u4ef6\u8bfb\u53d6\uff1a<\/p>\n<pre>function readFile($filename) {\n    $handle = fopen($filename, \"r\");\n    while (($line = fgets($handle)) !== false) {\n        yield $line;\n    }\n    fclose($handle);\n}\n\n$coroutine = readFile(\"test.txt\");\nwhile ($line = $coroutine-&gt;current()) {\n    echo $line;\n    $coroutine-&gt;next();\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u534f\u7a0b<code>readFile()<\/code>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u4ee5\u5e76\u53d1\u65b9\u5f0f\u8fd4\u56de\u6bcf\u884c\u3002\u4e3b\u7a0b\u5e8f\u53ef\u4ee5\u8fed\u4ee3\u534f\u7a0b\uff0c\u4f9d\u6b21\u83b7\u53d6\u6bcf\u884c\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86PHP\u4e2d\u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u7684\u5173\u952e\u51fd\u6570\u548c\u6982\u5ff5\u3002\u901a\u8fc7\u4f7f\u7528\u534f\u7a0b\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u7f16\u5199\u5e76\u53d1\u7684\u4ee3\u7801\uff0c\u4ee5\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3\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>\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3 \u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u300a\u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3\u300b\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01 \u5e76\u53d1\u7f16\u7a0b\u521d\u63a2\uff1aPHP\u51fd\u6570\u7684\u4f7f\u7528\u8be6\u89e3 PHP\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u63d0\u5347\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPHP \u4e2d\u7528\u4e8e\u5e76\u53d1\u7f16\u7a0b\u7684\u5173\u952e\u51fd\u6570\u3002 \u534f\u7a0b \u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u4ee3\u7801\u3002PHP 8.1\u5f15\u5165\u4e86\u534f\u7a0b\uff0c\u4f7f\u7528\\Generator\u5173\u952e\u5b57\u5b9a\u4e49\uff1a function myCoroutine() { yield &#8220;Hello&#8221;; } $coroutine = myCoroutine(); echo $coroutine-&gt;current(); \/\/ Output: Hello \u5e76\u53d1\u51fd\u6570 PHP\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5e76\u53d1\u51fd\u6570\uff1a coroutine_create(): \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\u3002 coroutine_resume(): \u6062\u590d\u4e00\u4e2a\u6302\u8d77\u7684\u534f\u7a0b\u3002 coroutine_yield(): \u6302\u8d77\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 coroutine_close(): \u9500\u6bc1\u4e00\u4e2a\u534f\u7a0b\u3002 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u6f14\u793a\u4e86\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5e76\u53d1\u6587\u4ef6\u8bfb\u53d6\uff1a function readFile($filename) { $handle = fopen($filename, &#8220;r&#8221;); while (($line = fgets($handle)) !== false) { yield $line; } fclose($handle); } $coroutine = [&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-46706","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46706","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=46706"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46706\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}