{"id":47493,"date":"2024-12-02T16:11:10","date_gmt":"2024-12-02T08:11:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47493\/"},"modified":"2024-12-02T16:11:10","modified_gmt":"2024-12-02T08:11:10","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e4%b8%80%e4%b8%aa%e5%bc%82%e6%ad%a5%e7%9a%84-php-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47493\/","title":{"rendered":"\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>PHP \u4e2d\u7f16\u5199\u5f02\u6b65\u51fd\u6570\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528 Promise \u6216 Generators\u3002Promise \u4ee3\u8868\u672a\u6765\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 Prooph\\Common\\Messaging\\Promise \u521b\u5efa\u3002Generators \u4f7f\u7528 yield \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u51fd\u6570\u6682\u505c\u5e76\u6062\u590d\u6267\u884c\u3002\u4f7f\u7528 Promise \u6216 Generators \u7684\u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0cPromise \u7528\u4e8e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5185\u5bb9\uff0c\u800c Generators \u7528\u4e8e\u629b\u51fa\u5f02\u5e38\u6216\u8fd4\u56de\u5185\u5bb9\u3002\u603b\u7684\u6765\u8bf4\uff0c\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729920752671c7ef0e3b4d.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570\u63d2\u56fe\" alt=\"\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u5229\u7528 Promise \u548c Generators \u7f16\u5199\u5f02\u6b65\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65 PHP \u51fd\u6570\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u4f7f\u7528 Promise<\/strong><\/p>\n<p>Promise \u662f\u4ee3\u8868\u672a\u6765\u503c\u7684\u5360\u4f4d\u7b26\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Prooph\\Common\\Messaging\\Promise<\/code> \u521b\u5efa Promise\uff1a<\/p>\n<pre>use Prooph\\Common\\Messaging\\Promise;\n\n$promise = new Promise(function ($resolve, $reject) {\n    \/\/ \u5f02\u6b65\u64cd\u4f5c\n    \/\/ ...\n\n    $resolve($result);\n});<\/pre>\n<p><strong>\u4f7f\u7528 Generators<\/strong><\/p>\n<p>Generators \u662f\u5141\u8bb8\u51fd\u6570\u6682\u505c\u6267\u884c\u5e76\u6062\u590d\u6267\u884c\u7684\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 yield \u5173\u952e\u5b57\u521b\u5efa Generator\uff1a<\/p>\n<pre>function asyncFunction() {\n    \/\/ \u5f02\u6b65\u64cd\u4f5c\n    \/\/ ...\n\n    yield $result;\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Promise \u4ece\u6587\u4ef6\u4e2d\u5f02\u6b65\u8bfb\u53d6\u5185\u5bb9\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>use Prooph\\Common\\Messaging\\Promise;\n\n$promise = new Promise(function ($resolve, $reject) {\n    file_get_contents('file.txt', function ($contents) use ($resolve, $reject) {\n        if (!$contents) {\n            $reject(new \\Exception(\"\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\"));\n        }\n\n        $resolve($contents);\n    });\n});\n\n$promise-&gt;then(function ($contents) {\n    \/\/ \u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u5904\u7406\n});<\/pre>\n<p><strong>\u4f7f\u7528 Generator<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 Generator \u7f16\u5199\u76f8\u540c\u7684\u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6\u51fd\u6570\uff1a<\/p>\n<pre>function readFile() {\n    $contents = file_get_contents('file.txt');\n\n    if (!$contents) {\n        throw new \\Exception(\"\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\");\n    }\n\n    yield $contents;\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 Promise \u6216 Generators\uff0c\u53ef\u4ee5\u5728 PHP \u4e2d\u7f16\u5199\u5f02\u6b65\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,\u5f02\u6b65\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570 \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 PHP \u4e2d\u7f16\u5199\u5f02\u6b65\u51fd\u6570\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528 Promise \u6216 Generators\u3002Promise \u4ee3\u8868\u672a\u6765\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 Prooph\\Common\\Messaging\\Promise \u521b\u5efa\u3002Generators \u4f7f\u7528 yield \u5173\u952e\u5b57\uff0c\u5141\u8bb8\u51fd\u6570\u6682\u505c\u5e76\u6062\u590d\u6267\u884c\u3002\u4f7f\u7528 Promise \u6216 Generators \u7684\u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0cPromise \u7528\u4e8e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5185\u5bb9\uff0c\u800c Generators \u7528\u4e8e\u629b\u51fa\u5f02\u5e38\u6216\u8fd4\u56de\u5185\u5bb9\u3002\u603b\u7684\u6765\u8bf4\uff0c\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002 \u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65\u7684 PHP \u51fd\u6570 \u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u5229\u7528 Promise \u548c Generators \u7f16\u5199\u5f02\u6b65\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u5f02\u6b65 PHP \u51fd\u6570\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002 \u4f7f\u7528 Promise Promise \u662f\u4ee3\u8868\u672a\u6765\u503c\u7684\u5360\u4f4d\u7b26\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Prooph\\Common\\Messaging\\Promise \u521b\u5efa Promise\uff1a use Prooph\\Common\\Messaging\\Promise; $promise = new Promise(function ($resolve, $reject) [&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-47493","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47493","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=47493"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47493\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}