{"id":327188,"date":"2022-01-10T08:47:42","date_gmt":"2022-01-10T08:47:42","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=327188"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=327188","title":{"rendered":"<span>\u201cYield\u201d \u0438 \u0434\u0435\u043b\u0438\u043a\u0430\u0442\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 PHP<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c4f\/92c\/1b7\/c4f92c1b77fdb18eb5af690884832ffd.jpeg\" alt=\"https:\/\/pixabay.com\" title=\"https:\/\/pixabay.com\" width=\"1250\" height=\"833\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c4f\/92c\/1b7\/c4f92c1b77fdb18eb5af690884832ffd.jpeg\" data-blurred=\"true\"\/><figcaption>https:\/\/pixabay.com<\/figcaption><\/figure>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c: \u201c\u041a\u0430\u043a\u0430\u044f \u043f\u043e\u043b\u044c\u0437\u0430 \u043e\u0442 <strong><em>yield<\/em><\/strong> \u0432 PHP?\u201d. \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0430\u0441 \u043e\u0442 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 Google; \u042f \u0441 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u0440\u0430\u0441\u043a\u0440\u043e\u044e \u0432\u0430\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432 \u043e <strong><em>yield<\/em><\/strong>:<\/p>\n<ol>\n<li>\n<p>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <strong><em>yield<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 <strong><em>yield <\/em><\/strong>\u0438 <strong><em>return<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong><em>yield<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0441\u044b\u043b\u043a\u0438.<\/p>\n<\/li>\n<\/ol>\n<h3>1. \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u201cyield\u201d<\/h3>\n<blockquote>\n<p><em>\u0424\u0443\u043d\u043a\u0446\u0438\u044f-\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u044b\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 (yields) \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u043e.<\/em><\/p>\n<\/blockquote>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"php\">function getValues() {    yield 'value'; } \/\/ \u0432\u044b\u0432\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0438 \"value\" echo getValues();<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0434\u0430\u0441\u0442 \u043e\u0448\u0438\u0431\u043a\u0443: <code>Object of class Generator could not be converted to string<\/code>. \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043f\u043e\u0447\u0435\u043c\u0443:<\/p>\n<h3>2. \u0420\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u201cyield\u201d \u0438 \u201creturn\u201d<\/h3>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>getValues\u200b\u200b()<\/code> \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u044d\u0442\u043e\u0433\u043e \u043e\u0436\u0438\u0434\u0430\u0442\u044c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0435\u0435 \u0442\u0438\u043f:<\/p>\n<pre><code class=\"php\">function getValues() {    return 'value'; } var_dump(getValues()); \/\/ string(5) \"value\" function getValues() {    yield 'value'; } var_dump(getValues()); \/\/ class Gene(0) {}rator#1 <\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <a href=\"http:\/\/php.net\/manual\/en\/class.generator.php\"><strong><u>Generator<\/u><\/strong><\/a> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <a href=\"http:\/\/php.net\/manual\/en\/class.iterator.php\"><strong><u>Iterator<\/u><\/strong><\/a>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>getValue()<\/code>:<\/p>\n<pre><code class=\"php\">foreach (getValues() as $value) {   echo $value; } \/\/ \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e $values = getValues(); foreach ($values as $value) {   echo $value; }<\/code><\/pre>\n<p>\u041d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f!<\/p>\n<blockquote>\n<p><em>\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 foreach \u0434\u043b\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432, \u0447\u0442\u043e, \u043e\u0434\u043d\u0430\u043a\u043e, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u044e \u043b\u0438\u043c\u0438\u0442\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/em><\/p>\n<\/blockquote>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 800000 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u0435\u0440\u043d\u0435\u043c \u0435\u0433\u043e \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>getValues\u200b\u200b()<\/code>, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u044f \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <a href=\"http:\/\/php.net\/manual\/en\/function.memory-get-usage.php\"><u>memory_get_usage()<\/u><\/a>. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0436\u0434\u044b\u0435 200000 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u0447\u0435\u0442\u044b\u0440\u0435:<\/p>\n<pre><code class=\"php\">&lt;?php function getValues() {    $valuesArray = [];    \/\/ get the initial memory usage    echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB' . PHP_EOL;    for ($i = 1; $i &lt; 800000; $i++) {       $valuesArray[] = $i;        \/\/ let us do profiling, so we measure the memory usage       if (($i % 200000) == 0) {          \/\/ get memory usage in megabytes          echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB'. PHP_EOL;       }    }    return $valuesArray; }  $myValues = getValues(); \/\/ building the array here once we call the function foreach ($myValues as $value) {}<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435:<\/p>\n<pre><code>0.34 MB 8.35 MB 16.35 MB 32.35 MB<\/code><\/pre>\n<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 30 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442 \u043f\u0430\u043c\u044f\u0442\u0438. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 <code>$valuesArray<\/code>, \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <strong><em>yield<\/em><\/strong>:<\/p>\n<pre><code class=\"php\">&lt;?php function getValues() {    \/\/ get the initial memory usage    echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB' . PHP_EOL;    for ($i = 1; $i &lt; 800000; $i++) {       yield $i;        \/\/ let us do profiling, so we measure the memory usage       if (($i % 200000) == 0) {          \/\/ get memory usage in megabytes          echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB'. PHP_EOL;       }    } }  $myValues = getValues(); \/\/ no action taken until we loop over the values foreach ($myValues as $value) {} \/\/ start generating values here<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u0441 \u043f\u043e\u0440\u0430\u0437\u0438\u0442\u044c:<\/p>\n<pre><code>0.34 MB 0.34 MB 0.34 MB 0.34 MB<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0442 <strong><em>return<\/em><\/strong> \u043a <strong><em>yield<\/em><\/strong>, \u043d\u043e \u0435\u0441\u043b\u0438 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, <strong><em>yield<\/em><\/strong> \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0432\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h3>3. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u201cyield\u201d<\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong><em>yield<\/em><\/strong>, \u043d\u043e \u044f \u0432\u044b\u0434\u0435\u043b\u044e \u043f\u0430\u0440\u0443 \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<ul>\n<li>\n<p>a. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <strong><em>yield<\/em><\/strong>, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong><em>return<\/em><\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"php\">function getValues() {   yield 'value';   return 'returnValue'; } $values = getValues(); foreach ($values as $value) {} echo $values->getReturn(); \/\/ 'returnValue'<\/code><\/pre>\n<ul>\n<li>\n<p>b. \u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"php\">function getValues() {   yield 'key' => 'value'; } $values = getValues(); foreach ($values as $key => $value) {   echo $key . ' => ' . $value; }<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/php.net\/manual\/en\/language.generators.syntax.php\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<h3>4. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0446\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 &#8212; \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u0432 \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 <strong><em>yield<\/em><\/strong> \u0438 <strong><em>return<\/em><\/strong> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u043e \u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e, \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443.<\/p>\n<h3>5. \u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ol>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/language.generators.syntax.php\"><u>http:\/\/php.net\/manual\/en\/language.generators.syntax.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/class.generator.php\"><u>http:\/\/php.net\/manual\/en\/class.generator.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/language.generators.php\"><u>http:\/\/php.net\/manual\/en\/language.generators.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/function.memory-get-usage.php\"><u>http:\/\/php.net\/manual\/en\/function.memory-get-usage.php<\/u><\/a><\/p>\n<\/li>\n<\/ol>\n<hr\/>\n<p><em>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 PHP Developer. Basic. \u0423\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435 <\/em><a href=\"https:\/\/otus.pw\/VjBc\/\"><em>\u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/em><\/a><em>.<\/em><\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/598577\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/598577\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption>https:\/\/pixabay.com<\/figcaption><\/figure>\n<p>\u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c: \u201c\u041a\u0430\u043a\u0430\u044f \u043f\u043e\u043b\u044c\u0437\u0430 \u043e\u0442 <strong><em>yield<\/em><\/strong> \u0432 PHP?\u201d. \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0430\u0441 \u043e\u0442 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 Google; \u042f \u0441 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u0440\u0430\u0441\u043a\u0440\u043e\u044e \u0432\u0430\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432 \u043e <strong><em>yield<\/em><\/strong>:<\/p>\n<ol>\n<li>\n<p>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <strong><em>yield<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 <strong><em>yield <\/em><\/strong>\u0438 <strong><em>return<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong><em>yield<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0441\u044b\u043b\u043a\u0438.<\/p>\n<\/li>\n<\/ol>\n<h3>1. \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u201cyield\u201d<\/h3>\n<blockquote>\n<p><em>\u0424\u0443\u043d\u043a\u0446\u0438\u044f-\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u044b\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 (yields) \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u043e.<\/em><\/p>\n<\/blockquote>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"php\">function getValues() {    yield 'value'; } \/\/ \u0432\u044b\u0432\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0438 \"value\" echo getValues();<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0434\u0430\u0441\u0442 \u043e\u0448\u0438\u0431\u043a\u0443: <code>Object of class Generator could not be converted to string<\/code>. \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043f\u043e\u0447\u0435\u043c\u0443:<\/p>\n<h3>2. \u0420\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u201cyield\u201d \u0438 \u201creturn\u201d<\/h3>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>getValues\u200b\u200b()<\/code> \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u044d\u0442\u043e\u0433\u043e \u043e\u0436\u0438\u0434\u0430\u0442\u044c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0435\u0435 \u0442\u0438\u043f:<\/p>\n<pre><code class=\"php\">function getValues() {    return 'value'; } var_dump(getValues()); \/\/ string(5) \"value\" function getValues() {    yield 'value'; } var_dump(getValues()); \/\/ class Gene(0) {}rator#1 <\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <a href=\"http:\/\/php.net\/manual\/en\/class.generator.php\"><strong><u>Generator<\/u><\/strong><\/a> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <a href=\"http:\/\/php.net\/manual\/en\/class.iterator.php\"><strong><u>Iterator<\/u><\/strong><\/a>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>getValue()<\/code>:<\/p>\n<pre><code class=\"php\">foreach (getValues() as $value) {   echo $value; } \/\/ \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e $values = getValues(); foreach ($values as $value) {   echo $value; }<\/code><\/pre>\n<p>\u041d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f!<\/p>\n<blockquote>\n<p><em>\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 foreach \u0434\u043b\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432, \u0447\u0442\u043e, \u043e\u0434\u043d\u0430\u043a\u043e, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u044e \u043b\u0438\u043c\u0438\u0442\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/em><\/p>\n<\/blockquote>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 800000 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u0435\u0440\u043d\u0435\u043c \u0435\u0433\u043e \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>getValues\u200b\u200b()<\/code>, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u044f \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <a href=\"http:\/\/php.net\/manual\/en\/function.memory-get-usage.php\"><u>memory_get_usage()<\/u><\/a>. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0436\u0434\u044b\u0435 200000 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u0447\u0435\u0442\u044b\u0440\u0435:<\/p>\n<pre><code class=\"php\">&lt;?php function getValues() {    $valuesArray = [];    \/\/ get the initial memory usage    echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB' . PHP_EOL;    for ($i = 1; $i &lt; 800000; $i++) {       $valuesArray[] = $i;        \/\/ let us do profiling, so we measure the memory usage       if (($i % 200000) == 0) {          \/\/ get memory usage in megabytes          echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB'. PHP_EOL;       }    }    return $valuesArray; }  $myValues = getValues(); \/\/ building the array here once we call the function foreach ($myValues as $value) {}<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435:<\/p>\n<pre><code>0.34 MB 8.35 MB 16.35 MB 32.35 MB<\/code><\/pre>\n<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 30 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442 \u043f\u0430\u043c\u044f\u0442\u0438. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 <code>$valuesArray<\/code>, \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <strong><em>yield<\/em><\/strong>:<\/p>\n<pre><code class=\"php\">&lt;?php function getValues() {    \/\/ get the initial memory usage    echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB' . PHP_EOL;    for ($i = 1; $i &lt; 800000; $i++) {       yield $i;        \/\/ let us do profiling, so we measure the memory usage       if (($i % 200000) == 0) {          \/\/ get memory usage in megabytes          echo round(memory_get_usage() \/ 1024 \/ 1024, 2) . ' MB'. PHP_EOL;       }    } }  $myValues = getValues(); \/\/ no action taken until we loop over the values foreach ($myValues as $value) {} \/\/ start generating values here<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u0441 \u043f\u043e\u0440\u0430\u0437\u0438\u0442\u044c:<\/p>\n<pre><code>0.34 MB 0.34 MB 0.34 MB 0.34 MB<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0442 <strong><em>return<\/em><\/strong> \u043a <strong><em>yield<\/em><\/strong>, \u043d\u043e \u0435\u0441\u043b\u0438 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, <strong><em>yield<\/em><\/strong> \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0432\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h3>3. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u201cyield\u201d<\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong><em>yield<\/em><\/strong>, \u043d\u043e \u044f \u0432\u044b\u0434\u0435\u043b\u044e \u043f\u0430\u0440\u0443 \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<ul>\n<li>\n<p>a. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <strong><em>yield<\/em><\/strong>, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong><em>return<\/em><\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"php\">function getValues() {   yield 'value';   return 'returnValue'; } $values = getValues(); foreach ($values as $value) {} echo $values->getReturn(); \/\/ 'returnValue'<\/code><\/pre>\n<ul>\n<li>\n<p>b. \u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"php\">function getValues() {   yield 'key' => 'value'; } $values = getValues(); foreach ($values as $key => $value) {   echo $key . ' => ' . $value; }<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/php.net\/manual\/en\/language.generators.syntax.php\"><u>\u0437\u0434\u0435\u0441\u044c<\/u><\/a>.<\/p>\n<h3>4. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0446\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 &#8212; \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u0432 \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 <strong><em>yield<\/em><\/strong> \u0438 <strong><em>return<\/em><\/strong> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u043e \u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e, \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443.<\/p>\n<h3>5. \u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ol>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/language.generators.syntax.php\"><u>http:\/\/php.net\/manual\/en\/language.generators.syntax.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/class.generator.php\"><u>http:\/\/php.net\/manual\/en\/class.generator.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/language.generators.php\"><u>http:\/\/php.net\/manual\/en\/language.generators.php<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/php.net\/manual\/en\/function.memory-get-usage.php\"><u>http:\/\/php.net\/manual\/en\/function.memory-get-usage.php<\/u><\/a><\/p>\n<\/li>\n<\/ol>\n<hr\/>\n<p><em>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 PHP Developer. Basic. \u0423\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435 <\/em><a href=\"https:\/\/otus.pw\/VjBc\/\"><em>\u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/em><\/a><em>.<\/em><\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/598577\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/598577\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-327188","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=327188"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327188\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=327188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=327188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=327188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}