{"id":317591,"date":"2021-02-07T15:00:13","date_gmt":"2021-02-07T15:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=317591"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=317591","title":{"rendered":"Enum \u0432 PHP 8.1 \u2014 \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0443\u0436\u0435\u043d enum, \u0438 \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 PHP"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 <a href=\"https:\/\/wiki.php.net\/rfc\/enumerations#voting\" rel=\"nofollow noopener noreferrer\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 enum \u0432 PHP 8.1 <\/a>. \u0423\u0436\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u201c\u0437\u0430\u201d \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043a\u0440\u0430\u0442\u043a\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0430\u0432\u0442\u043e\u0440\u044b \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  <\/p>\n<h2 id=\"zachem-nuzhny-enum\">\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b enum?<\/h2>\n<p>  <\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0443\u0436\u043d\u044b enum? \u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u043d\u0438 \u0441\u043b\u0443\u0436\u0430\u0442 \u0446\u0435\u043b\u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0435\u0437 \u0435\u043d\u0443\u043c\u043e\u0432 \u0438 \u0441 \u043d\u0438\u043c\u0438. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0430\u0448\u0438\u043d\u044b \u0442\u0440\u0435\u0445 \u0446\u0432\u0435\u0442\u043e\u0432: \u043a\u0440\u0430\u0441\u043d\u044b\u0435, \u0447\u0435\u0440\u043d\u044b\u0435 \u0438 \u0431\u0435\u043b\u044b\u0435. \u041a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0446\u0432\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u044b\u0431\u0440\u0430\u0442\u044c?<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">class Car {     private string $color;      function setColor(string $color): void {         $this-&gt;color = $color;     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043e\u043f\u0438\u0448\u0435\u043c \u0446\u0432\u0435\u0442 \u043c\u0430\u0448\u0438\u043d\u044b \u043a\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e\u0439 string, \u0442\u043e \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 $myCar-&gt;setColor(..) \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0437\u0430 \u0441\u0442\u0440\u043e\u043a\u0443 \u0442\u0443\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u044c. \u201cred\u201d \u0438\u043b\u0438 \u201cRED\u201d \u0438\u043b\u0438 \u201c#ff0000\u201d, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u044b\u0445, \u043b\u0435\u0433\u043a\u043e \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f, \u043f\u0440\u043e\u0441\u0443\u043d\u0443\u0432 \u0442\u0443\u0434\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0442\u043e (\u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443). \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u0447\u0438\u0441\u043b\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. <\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u042d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 php-\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u0437\u0430\u0432\u043e\u0434\u044f\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442 \u0438\u0445 \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u044f\u0432\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b. <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">class Color {     public const RED   = &quot;red&quot;;     public const BLACK = &quot;black&quot;;     public const WHITE = &quot;white&quot;; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438 \u0437\u0430\u0434\u0430\u0432\u0430\u044f \u0446\u0432\u0435\u0442, \u043f\u0438\u0448\u0443\u0442 <code>$myCar-&gt;setColor(Color::RED);<\/code><\/p>\n<p>  <\/p>\n<p>\u0423\u0436\u0435 \u043b\u0443\u0447\u0448\u0435. \u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0432\u0438\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434 $myCar-&gt;setColor(&#8230;), \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438 \u043d\u0435 \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0433\u0434\u0435-\u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0434\u043b\u044f \u0446\u0432\u0435\u0442\u043e\u0432. \u0418 \u043c\u044b \u0432\u0441\u0451 \u0435\u0449\u0435 \u043c\u043e\u0436\u0435\u043c \u0441\u0443\u043d\u0443\u0442\u044c \u0442\u0443\u0434\u0430 \u043b\u044e\u0431\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0431\u0435\u0437 \u043a\u0430\u043a\u043e\u0433\u043e-\u043b\u0438\u0431\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435. <\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u0435\u043d \u043d\u0435 \u043a\u043b\u0430\u0441\u0441, \u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f. \u042d\u0442\u043e\u0442 \u0442\u0438\u043f \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f enum<\/p>\n<p>  <\/p>\n<h2 id=\"pure-enum\">Pure enum<\/h2>\n<p>  <\/p>\n<p>\u0412 \u0441\u0430\u043c\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 (pure enum), enum \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Color {     case Red;     case Black;     case White; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043f\u0438\u0441\u0430\u0432 \u0442\u0430\u043a\u043e\u0439 \u0442\u0438\u043f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u0435\u0437\u0434\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">class Car {     private Color $color;      function setColor(Color $color): void {         $this-&gt;color = $color;     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u0437 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u0441\u0451 \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u043d\u043e, \u043a\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0435\u0441\u0442\u044c. \u0418 \u043c\u0435\u0442\u043e\u0434 setColor \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a: $myCar-&gt;setColor(Color::White), \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u0438 \u0434\u043e\u043c\u043e\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442. \u041d\u0438\u0447\u0435\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043d\u0435 \u0441\u0443\u043d\u0435\u0448\u044c. \u0427\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0441\u0442\u0430\u043b\u0430 \u0432\u044b\u0448\u0435. <\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 case-\u043e\u0432 (Color::Red, Color::Black, Color::White) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0442\u0438\u043f\u0430 Color (\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 instanseof ). \u0422.\u0435. \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u044d\u0442\u043e \u043d\u0435 \u0447\u0438\u0441\u043b\u0430 0,1,2 \u043a\u0430\u043a \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u0418\u0445 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c &gt;, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0435\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e $name:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">print Color::Red-&gt;name; \/\/ \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u201cRed\u201d<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"enum-so-skalyarami\">Enum \u0441\u043e \u0441\u043a\u0430\u043b\u044f\u0440\u0430\u043c\u0438<\/h2>\n<p>  <\/p>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u0432\u0441\u0451, \u0442\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e. \u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f enum \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u044b\u0439 \u0442\u0438\u043f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 string. \u0418 \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u0435\u0439\u0441\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0446\u0435\u043b\u0435\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0438\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Color: string {     case Red = &quot;R&quot;;     case Black = &quot;B&quot;;     case White = &quot;W&quot;; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043a\u0430\u043b\u044f\u0440\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043c \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">Color::Red-&gt;value  \/\/\u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u201cR\u201d<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0442.\u0435. \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c case \u0447\u0435\u0440\u0435\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">Color::from(&quot;R&quot;) \/\/ \u0432\u0435\u0440\u043d\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Color::Red<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u043f\u043e\u043b\u0435\u0439 &quot;case&quot; \u0432 enum \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e. \u041f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u0440\u0430\u0437\u043d\u043e\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0430. \u0422\u0430\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0439\u0442\u044b.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 RFC.<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">interface Colorful {   public function color(): string; }  trait Rectangle {   public function shape(): string {     return &quot;Rectangle&quot;;   } }  enum Suit implements Colorful {   use Rectangle;    case Hearts;   case Diamonds;   case Clubs;   case Spades;    public function color(): string {     return match($this) {       Suit::Hearts, Suit::Diamonds =&gt; 'Red',       Suit::Clubs, Suit::Spaces =&gt; 'Black',     };   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c $this \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 case, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434. <\/p>\n<p>  <\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0435\u043d\u0430\u043c\u0430\u043c\u0438 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 match. \u041f\u043e\u0445\u043e\u0436\u0435, match \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u043d\u0438\u0445.<\/p>\n<p>  <\/p>\n<p>\u041b\u0438\u0447\u043d\u043e \u044f \u0433\u043e\u0440\u044f\u0447\u043e \u043e\u0434\u043e\u0431\u0440\u044f\u044e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 enum \u0432 PHP, \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0438 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e, \u0438 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u043e\u0432, \u0433\u0434\u0435 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u0442\u0438\u043f\u044b, enum \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0435\u0441\u0442\u044c (\u043a\u0440\u043e\u043c\u0435, \u0440\u0430\u0437\u0432\u0435 \u0447\u0442\u043e Go).<\/p>\n<p>  <\/p>\n<h2 id=\"dalshe---bolshe-tagged-unions-tip-summa\">\u0414\u0430\u043b\u044c\u0448\u0435 \u2014 \u0431\u043e\u043b\u044c\u0448\u0435. Tagged Unions (\u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u0430)<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c RFC, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u044e\u0442 \u0438\u0434\u0435\u044e enums \u0434\u0430\u043b\u044c\u0448\u0435, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u043c enum \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432. \u041a\u0430\u043a \u0432 \u044f\u0437\u044b\u043a\u0435 Rust, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, enum Result \u0441 \u0434\u0432\u0443\u043c\u044f case-\u0430\u043c\u0438 Result::Ok \u0438 Result::Err, \u043f\u0440\u0438\u0447\u0435\u043c \u044d\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435: Ok \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0430 Err \u2014 \u043e\u0448\u0438\u0431\u043a\u0443, \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u0432\u043e\u0439 \u0442\u0438\u043f. <\/p>\n<p>  <\/p>\n<p>\u0418 \u0432\u0441\u0451 \u044d\u0442\u043e \u043d\u0435 \u0432 \u0420\u0430\u0441\u0442\u0435 \u0438\u043b\u0438 \u0425\u0430\u0441\u043a\u0435\u043b\u0435, \u0430 \u0432 PHP!<\/p>\n<p>  <\/p>\n<p>\u041e\u0431 \u044d\u0442\u043e\u043c \u043c\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u043c \u0432 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 \u0432 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u043f\u043e\u0441\u0442\u0430\u0445 <a href=\"https:\/\/t.me\/crossjoin\" rel=\"nofollow noopener noreferrer\">\u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u043a\u0430\u043d\u0430\u043b\u0430 Cross Join<\/a>, \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f.<\/p>\n<\/div>\n<p> \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\/post\/541246\/\"> https:\/\/habr.com\/ru\/post\/541246\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 <a href=\"https:\/\/wiki.php.net\/rfc\/enumerations#voting\" rel=\"nofollow noopener noreferrer\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 enum \u0432 PHP 8.1 <\/a>. \u0423\u0436\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u201c\u0437\u0430\u201d \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043a\u0440\u0430\u0442\u043a\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0430\u0432\u0442\u043e\u0440\u044b \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  <\/p>\n<h2 id=\"zachem-nuzhny-enum\">\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b enum?<\/h2>\n<p>  <\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0443\u0436\u043d\u044b enum? \u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u043d\u0438 \u0441\u043b\u0443\u0436\u0430\u0442 \u0446\u0435\u043b\u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0435\u0437 \u0435\u043d\u0443\u043c\u043e\u0432 \u0438 \u0441 \u043d\u0438\u043c\u0438. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0430\u0448\u0438\u043d\u044b \u0442\u0440\u0435\u0445 \u0446\u0432\u0435\u0442\u043e\u0432: \u043a\u0440\u0430\u0441\u043d\u044b\u0435, \u0447\u0435\u0440\u043d\u044b\u0435 \u0438 \u0431\u0435\u043b\u044b\u0435. \u041a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0446\u0432\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u044b\u0431\u0440\u0430\u0442\u044c?<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">class Car {     private string $color;      function setColor(string $color): void {         $this-&gt;color = $color;     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043e\u043f\u0438\u0448\u0435\u043c \u0446\u0432\u0435\u0442 \u043c\u0430\u0448\u0438\u043d\u044b \u043a\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e\u0439 string, \u0442\u043e \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 $myCar-&gt;setColor(..) \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0437\u0430 \u0441\u0442\u0440\u043e\u043a\u0443 \u0442\u0443\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u044c. \u201cred\u201d \u0438\u043b\u0438 \u201cRED\u201d \u0438\u043b\u0438 \u201c#ff0000\u201d, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u044b\u0445, \u043b\u0435\u0433\u043a\u043e \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f, \u043f\u0440\u043e\u0441\u0443\u043d\u0443\u0432 \u0442\u0443\u0434\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0442\u043e (\u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443). \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u0447\u0438\u0441\u043b\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. <\/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-317591","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/317591","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=317591"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/317591\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}