{"id":418728,"date":"2021-03-14T08:57:06","date_gmt":"2021-03-14T00:57:06","guid":{"rendered":"http:\/\/4563.org\/?p=418728"},"modified":"2021-03-14T08:57:06","modified_gmt":"2021-03-14T00:57:06","slug":"%e8%af%b7%e6%95%99%e4%b8%8b-php-%e5%85%b3%e4%ba%8e%e5%8d%8f%e7%a8%8b%e7%9a%84%e4%b8%80%e4%ba%9b%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=418728","title":{"rendered":"\u8bf7\u6559\u4e0b PHP \u5173\u4e8e\u534f\u7a0b\u7684\u4e00\u4e9b\u77e5\u8bc6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e0b PHP \u5173\u4e8e\u534f\u7a0b\u7684\u4e00\u4e9b\u77e5\u8bc6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : awanganddong <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>1.\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5185\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u65b9\u6cd5\u3002\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u751f\u6210\u5668\u81ea\u52a8\u5b9e\u73b0\u4e86\u201c\u8fed\u4ee3\u5668\u534f\u8bae\u201d\uff08\u5373__iter__\u548c next \u65b9\u6cd5\uff09\uff0c\u4e0d\u9700\u8981\u518d\u624b\u52a8\u5b9e\u73b0\u4e24\u65b9\u6cd5\u3002\uff08\u8fd9\u79cd\u8bf4\u6cd5\u4e0d\u77e5\u9053\u5bf9\u4e0d\u5bf9\uff09  2.php \u534f\u7a0b\u7684\u5b9e\u73b0\u901a\u8fc7 yield \u548c generator \u3002generator \u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u5b9e\u73b0\u7b80\u5355\u7684 Iterator \uff08\u8fed\u4ee3\u5668\uff09\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528 Generator \u5b9e\u73b0 Iterator \u4e0d\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u7ee7\u627f Iterator \u63a5\u53e3\u3002\uff08\u5bf9\u8fd9\u4e2a\u5177\u4f53\u5b9e\u73b0\uff0c\u80fd\u7528\u5927\u767d\u8bdd\u8bb2\u4e0b\u5417\uff09  3.Generator \uff08\u65e0\u5806\u6808\u8f7b\u91cf\u7ea7\u5e76\u53d1\u5b9e\u73b0\uff09\u8fd9\u91cc\u65e0\u5806\u6808\u8f7b\u91cf\u7ea7\u4f1a\u5b58\u5728\u4ec0\u4e48\u7f3a\u9677\uff1f   <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5581326\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SimbaPeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e0d\u662f Python?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5581327\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : awanganddong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53c2\u8003 \u5728 PHP \u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u591a\u4efb\u52a1\u8c03\u5ea6<\/p>\n<p>https:\/\/www.laruence.com\/2015\/05\/28\/3038.html<\/p>\n<p>\u6709\u4e9b\u542b\u4e49\u5728 php \u54ea\u91cc\u4e0d\u597d\u627e\uff0c\u5c31\u53bb\u770b\u770b\u9694\u58c1 python \u7684\u7406\u89e3\uff0c\u5e94\u8be5\u5927\u540c\u5c0f\u5f02\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5581328\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : awanganddong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>function gen($max)<br \/>{<br \/> for ($i=0; $i&lt;$max; $i++) {<br \/> yield $i;<br \/> }<br \/>}<\/p>\n<p>$gen = gen(5);<\/p>\n<p>foreach ($gen as $key=&gt;$val) {<br \/> var_dump($key . &#8220;=&gt;&#8221; . $val);<br \/>}<\/p>\n<p>\/\/\u4ee5\u4e0b\u662f\u5bf9\u5e94\u7684\u8f93\u51fa\u503c<br \/>string(4) &#8220;0=&gt;0&#8221;<br \/>string(4) &#8220;1=&gt;1&#8221;<br \/>string(4) &#8220;2=&gt;2&#8221;<br \/>string(4) &#8220;3=&gt;3&#8221;<br \/>string(4) &#8220;4=&gt;4&#8221;<\/p>\n<p>&#8220;`<\/p>\n<p>yield \u5177\u4f53\u7684\u6267\u884c\u6d41\u7a0b\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2a\u5e16\u5b50<\/p>\n<p>## PHP \u751f\u6210\u5668&#8211;\u52a8\u6001\u751f\u6210\u5185\u5bb9\u7684\u6570\u7ec4<br \/>https:\/\/segmentfault.com\/a\/1190000015149274                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5581329\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : awanganddong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>&lt;?php<\/p>\n<p>function gen($max)<br \/>{<br \/> for ($i=0; $i&lt;$max; $i++) {<br \/> yield $i=&gt;$i+1; (\u6ce8\u610f\u8fd9\u91cc\u662f=&gt;,\u800c\u4e0d\u662f=\u53f7\u3002\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u662f\u7b49\u53f7\u6682\u65f6\u4e0d\u660e)<br \/> }<br \/>}<\/p>\n<p>$gen = gen(5);<\/p>\n<p>foreach ($gen as $key=&gt;$val) {<br \/> var_dump($key . &#8220;=&gt;&#8221; . $val);<br \/>}<\/p>\n<p>\/\/\u4ee5\u4e0b\u662f\u8f93\u51fa\u7684\u503c<\/p>\n<p>string(4) &#8220;0=&gt;1&#8221;<br \/>string(4) &#8220;1=&gt;2&#8221;<br \/>string(4) &#8220;2=&gt;3&#8221;<br \/>string(4) &#8220;3=&gt;4&#8221;<br \/>string(4) &#8220;4=&gt;5&#8221;<\/p>\n<p>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5581330\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : awanganddong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>function gen() {<br \/> $ret = (yield &#8216;yield1&#8217;);<br \/> var_dump($ret);<br \/> echo &#8216;A1111&#8217;.PHP_EOL;<br \/> $ret = (yield &#8216;yield2&#8217;);<br \/> echo &#8220;B1111&#8221;.PHP_EOL;<br \/>}<br \/>$gen = gen();<br \/>var_dump($gen-&gt;current());<br \/>echo &#8216;A&#8217;.PHP_EOL;<br \/>var_dump($gen-&gt;send(&#8216;ret1&#8217;));<br \/>echo &#8216;B&#8217;.PHP_EOL;<\/p>\n<p>string(6) &#8220;yield1&#8221;<br \/>A<br \/>string(4) &#8220;ret1&#8221;<br \/>A1111<br \/>string(6) &#8220;yield2&#8221;<br \/>B<br \/>&#8220;`<br \/>\u4e0a\u8fb9\u8fd9\u4e2a\u4e0d\u592a\u7406\u89e3\u7684\u70b9\u662f$gen-&gt;send()\u8fd9\u91cc<br \/>\/\/\u7ed3\u8bba<br \/>send \u65b9\u6cd5\u5c06\u81ea\u5df1\u7684\u53d1\u9001\u7684\u503c\u4f20\u9012\u7ed9\u4e0a\u4e00\u4e2a yield \u8868\u8fbe\u5f0f\u7684\u7ed3\u679c,<br \/>\u7136\u540e\u7ee7\u7eed\u9690\u5f0f\u7684\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a yield \u5e76\u63a5\u6536\u4e0b\u4e00\u4e2a yield \u7684\u503c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5581331\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rqrq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/elarity\/advanced-php\/blob\/master\/17.%20PHP%E4%B8%AD%E7%9A%84yield%EF%BC%88%E4%B8%8A%EF%BC%89.md<\/p>\n<p>https:\/\/github.com\/elarity\/advanced-php\/blob\/master\/18.%E5%A1%AB%E5%9D%91%E4%B9%8BPHP%E7%9A%84yield%E5%92%8C%E5%8D%8F%E7%A8%8B%E5%9C%A8%E4%B8%80%E8%B5%B7%E7%9A%84%E6%97%A5%E5%AD%90%E9%87%8C.md<\/p>\n<p>\u8fd9\u91cc\u8bf4\u5f97\u6bd4\u8f83\u6e05\u695a\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e0b PHP \u5173\u4e8e\u534f\u7a0b\u7684\u4e00\u4e9b\u77e5\u8bc6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/418728"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=418728"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/418728\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=418728"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=418728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}