{"id":335875,"date":"2021-09-07T15:00:06","date_gmt":"2021-09-07T12:00:06","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/"},"modified":"2021-09-07T15:00:06","modified_gmt":"2021-09-07T12:00:06","slug":"how-to-use-enums-in-php-8-1-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/","title":{"rendered":"#How to Use Enums in PHP 8.1 \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a287a1f58d5d\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a287a1f58d5d\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#Basic_Syntax\" >Basic Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#Pure_vs_Backed_Enums\" >Pure vs Backed Enums<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#Adding_Methods_to_Enums\" >Adding Methods to Enums<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#Constants\" >Constants<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#When_to_Use_Enums\" >When to Use Enums?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-enums-in-php-8-1-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use Enums in PHP 8.1 \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-4047\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>PHP 8.1 will finally add language support for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/enumerations\">enums<\/a>. Enums, short for enumerations, are types which can only be instantiated with specific values. They\u2019re commonly found in other object-oriented languages but have previously required userland workarounds to implement in PHP.<\/p>\n<h2 id=\"basic-syntax\"><span class=\"ez-toc-section\" id=\"Basic_Syntax\"><\/span>Basic Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s what a simple enum looks like:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">case<\/span> Published<span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> InReview<span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>case<\/code> keyword, previously part of <code>switch<\/code> statements, is used to delineate the specific values the enum accepts. Values are referenced in the same way as class constants:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$published<\/span> <span class=\"sy0\">=<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Enums behave similarly to classes and interfaces. They\u2019re fully compatible with the type system, so you can typehint that a function only accepts a value defined in an enum:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> BlogPost <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Headline<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Content<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> PostStatus <span class=\"re0\">$Status<\/span><span class=\"sy0\">=<\/span>PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Draft<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Here\u2019s an example of using the <code>BlogPost<\/code> class:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ OK<\/span>\n<span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> BlogPost<span class=\"br0\">(<\/span>\n    <span class=\"st0\">\"Example Post\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"An example\"<\/span><span class=\"sy0\">,<\/span>\n    PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Draft<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ TypeError: Argument #3 ($Status) must be of type PostStatus<\/span>\n<span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> BlogPost<span class=\"br0\">(<\/span>\n    <span class=\"st0\">\"Broken Example\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"A broken example\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"Submitted\"<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The first instance works because its <code>$Status<\/code> is a valid value from the <code>PostStatus<\/code> enum. In the second case, a plain string is passed as <code>$Status<\/code>, which is prohibited as the value must be defined within <code>PostStatus<\/code>.<\/p>\n<p>Enum cases are represented as constants on the enum object. This means you can use them as static values and as part of constant expressions. The <code>BlogPost<\/code> constructor shows an enum case being used as a default parameter value, where <code>$Status<\/code> is automatically set to Draft when no value is supplied by the caller.<\/p>\n<p>You can access all the available values in an enum using its <code>cases<\/code> method:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">cases<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"co1\">\/\/ [PostStatus::Published, PostStatus::InReview, PostStatus::Draft]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"pure-vs-backed-enums\"><span class=\"ez-toc-section\" id=\"Pure_vs_Backed_Enums\"><\/span>Pure vs Backed Enums<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>PostStatus<\/code> enum above is a <em>pure<\/em> enum. It contains only <code>case<\/code> statements, with no extra data. PHP also lets you attach a value to enum cases, creating a <em>backed<\/em> enum.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">case<\/span> Published <span class=\"sy0\">=<\/span> <span class=\"st0\">\"S1\"<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> InReview <span class=\"sy0\">=<\/span> <span class=\"st0\">\"S2\"<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft <span class=\"sy0\">=<\/span> <span class=\"st0\">\"S3\"<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Here the <code>PostStatus<\/code> enum has been modified to create a backed enum. The typehint in the enum\u2019s definition stipulates that each case has a string value assigned to it. In this example, we\u2019re assuming each named post status has an associated short identifier. It might be this identifier that gets saved into the database when posts are persisted.<\/p>\n<p>You can access backed values via the <code>value<\/code> property on case instances:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> BlogPostRepository <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> save<span class=\"br0\">(<\/span>BlogPost <span class=\"re0\">$Post<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">insert<\/span><span class=\"br0\">(<\/span>\n            <span class=\"st0\">\"blog_posts\"<\/span><span class=\"sy0\">,<\/span>\n            <span class=\"br0\">[<\/span>\n                <span class=\"st0\">\"headline\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re0\">$Post<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Headline<\/span><span class=\"sy0\">,<\/span>\n                <span class=\"st0\">\"content\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re0\">$Post<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Content<\/span><span class=\"sy0\">,<\/span>\n                <span class=\"st0\">\"status\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re0\">$Post<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Status<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">value<\/span>\n            <span class=\"br0\">]<\/span>\n        <span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> BlogPost<span class=\"br0\">(<\/span><span class=\"st0\">\"Example\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Demo\"<\/span><span class=\"sy0\">,<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> BlogPostRepository<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">save<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$post<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This example would set the value of the persisted <code>status<\/code> field to <code>S1<\/code>, based on the backed version of the <code>PostStatus<\/code> enum shown above.<\/p>\n<p>Backed enums <em>only<\/em> accept strings and integers as values. It\u2019s not possible to use the union type <code>string|int<\/code> either. In addition, each case needs a unique value \u2013 the following example isn\u2019t permissible:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw1\">case<\/span> Published <span class=\"sy0\">=<\/span> <span class=\"st0\">\"S1\"<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft <span class=\"sy0\">=<\/span> <span class=\"st0\">\"S1\"<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>PHP provides a utility method on enums to create an instance from a backed value:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ fetch the blog post from earlier from the database<\/span>\n<span class=\"co1\">\/\/ the \"status\" field = S1<\/span>\n<span class=\"re0\">$status<\/span> <span class=\"sy0\">=<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">from<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$record<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"status\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>from()<\/code> method will hydrate instances from value cases. In this example, <code>S1<\/code> is m<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ed back to the <code>Published<\/code> case, and your code receives an instance of <code>PostStatus::Published<\/code>.<\/p>\n<p><code>from()<\/code> throws a <code>ValueError<\/code> if the input value is invalid; in scenarios where you know the value might not be usable, the alternative <code>tryFrom()<\/code> method can be used instead. This returns <code>null<\/code> when there\u2019s no match, instead of throwing the error.<\/p>\n<h2 id=\"adding-methods-to-enums\"><span class=\"ez-toc-section\" id=\"Adding_Methods_to_Enums\"><\/span>Adding Methods to Enums<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As enums are based on classes, you can also add methods to them!<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw1\">case<\/span> Published<span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft<span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> isPubliclyAccessible<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> bool <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$this<\/span> instanceof <span class=\"kw2\">self<\/span><span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This lets you keep case-specific behavior within your enum, instead of duplicating it across your codebase.<\/p>\n<p>Enums can implement interfaces too:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"kw2\">implements<\/span> PublicAccessGatable <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw1\">case<\/span> Published<span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft<span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> isPubliclyAccessible<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> bool <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$this<\/span> instanceof <span class=\"kw2\">self<\/span><span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now you can pass a <code>PostStatus<\/code> instance to anything which accepts a <code>PublicAccessGatable<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> UserAuthenticator <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">function<\/span> shouldAllowAccess<span class=\"br0\">(<\/span>PublicAccessGatable <span class=\"re0\">$Resource<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> bool <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">User<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">isAdmin<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">||<\/span> <span class=\"re0\">$Resource<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">isPubliclyAccessible<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$auth<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> UserAuthenticator<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ get a blog post from the database<\/span>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"sy0\">!<\/span><span class=\"re0\">$auth<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">shouldAllowAccess<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$post<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Status<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    http_response_code<span class=\"br0\">(<\/span><span class=\"nu0\">403<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>There are no restrictions on what you can do with enum methods \u2013 they are regular PHP methods, after all \u2013 but in <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> you\u2019d expect them to perform some kind of comparison against the instance\u2019s case, then return a static value. Enums can use traits, so you can pull in existing methods which you\u2019ve abstracted in this way too.<\/p>\n<p>You can use <code>public<\/code>, <code>protected<\/code> and <code>private<\/code> methods in enums, although <code>protected<\/code> and <code>private<\/code> have the same effect. Enums can\u2019t extend each other so <code>private<\/code> is effectively redundant. You can\u2019t add a constructor or destructor either. Static methods are supported and can be called on the enum class or its case instances.<\/p>\n<h2 id=\"constants\"><span class=\"ez-toc-section\" id=\"Constants\"><\/span>Constants<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Enums can also have their own constants, either as regular literal values or a reference to an enum case:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">enum PostStatus <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw1\">case<\/span> Published<span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Draft<span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> Live <span class=\"sy0\">=<\/span> <span class=\"kw2\">self<\/span><span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> PlainConstant <span class=\"sy0\">=<\/span> <span class=\"st0\">\"foobar\"<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This holds the potential to create confusion as the same syntax is used to access cases (enum instances) and constants:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$published<\/span> <span class=\"sy0\">=<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$plain<\/span> <span class=\"sy0\">=<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">PlainConstant<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Only <code>$published<\/code> would satisfy a <code>PostStatus<\/code> typehint, as <code>$plain<\/code> refers to a simple scalar value.<\/p>\n<h2 id=\"when-to-use-enums\"><span class=\"ez-toc-section\" id=\"When_to_Use_Enums\"><\/span>When to Use Enums?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Enums are for occasions where you need flexibility in the value a variable can take, but only among a predetermined set of possible cases.<\/p>\n<p>The blog post class that\u2019s run through this post is a classic example. Posts can only be in one of a known set of states but PHP previously had no straightforward way of achieving this.<\/p>\n<p>In older versions, you might have used this approach:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> PostStatus <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">const<\/span> Published <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">const<\/span> Draft <span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">class<\/span> BlogPost <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Headline<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> int <span class=\"re0\">$Status<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> BlogPost<span class=\"br0\">(<\/span><span class=\"st0\">\"My Headline\"<\/span><span class=\"sy0\">,<\/span> PostStatus<span class=\"sy0\">::<\/span><span class=\"me2\">Published<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The problem here is that <code>$Status<\/code> actually accepts any integer, so the following call would be perfectly valid:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> BlogPost<span class=\"br0\">(<\/span><span class=\"st0\">\"My Headline\"<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">9000<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Furthermore, <code>BlogPost<\/code> and <code>PostStatus<\/code> are completely detached \u2013 there\u2019s no way somebody reading <code>BlogPost<\/code> can learn the range of values that <code>$Status<\/code> <em>actually<\/em> accepts. While these issues can be mitigated by using appropriate docblock typehints, or third-party \u201cfake enum\u201d packages, they\u2019re all adding extra layers around a concept other programming languages make simple.<\/p>\n<p>Adding native enums to PHP is a step that helps to round off the language\u2019s type system. You can finally typehint permissible values in a way that keeps everyone on the same page. If you pass an invalid value, you\u2019ll get a runtime error. Your IDE can better assist you in supplying correct values, as it will know that <code>$Status<\/code> only accepts three options, instead of \u201cany integer.\u201d<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Enums address some common developer pain points when working in PHP. They make it possible to typehint that parameters, return values, and properties must be one of a set of predetermined options.<\/p>\n<p>Enums are flexible codebase entities which you can keep simple in pure form, or extend with backed values, interface implementations, and custom methods. Enums behave similarly to regular objects in most cases and support class features such as <code>__call()<\/code>, <code>__invoke<\/code>, and <code>::class<\/code>.<\/p>\n<p>You can introspect enums with the new <code>enum_exists()<\/code> function and <code>ReflectionEnum<\/code> Reflection class. In addition, enums implement two new interfaces, <code>UnitEnum<\/code> (in the case of pure enums), and <code>BackedEnum<\/code> (for enums with backed values). These can be used in generic framework code which works with <em>any<\/em> enum. The interfaces cannot be manually implemented by userland code.<\/p>\n<p>Enums will land in PHP as part of the 8.1 release in November 2021. They\u2019re already available in the latest beta builds. PHP 8.1 will ship several other convenience features too, including readonly properties and intersection types.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14076\/how-to-use-enums-in-php-8-1\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use Enums in PHP 8.1 \u2013 CloudSavvy IT&#8221; PHP 8.1 will finally add language support for enums. Enums, short for enumerations, are types which can only be instantiated with specific values. They\u2019re commonly found in other object-oriented languages but have previously required userland workarounds to implement in PHP. Basic Syntax Here\u2019s what a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":335876,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-335875","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/335875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=335875"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/335875\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/335876"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=335875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=335875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=335875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}