title: Create Enum from Name in PHP
body: <p>In PHP 8.1, <a href="https://www.php.net/manual/de/language.enumerations.backed.php" rel="nofollow noreferrer">BackedEnum</a> offer a <code>from</code> and <code>tryFrom</code> method to get an enum from a value. How can the same be achieved by non backed enums?</p>
  <p>Example BackedEnum:</p>
  <pre><code>enum MainType: string
  {
      case Full = 'a';
      case Major = 'b';
      case Minor = 'c';
  }
  
  var_dump(MainType::tryFrom('a')); // MainType::Full
  var_dump(MainType::tryFrom('d')); // null
  </code></pre>
  <p>However this doesn't exist for regular enums.</p>
  <p>How would I retrieve a &quot;normal&quot; Enum by name, like:</p>
  <pre><code>enum MainType
  {
      case Full;
      case Major;
      case Minor;
  }
  
  $name = (MainType::Full)-&gt;name
  var_dump(name); // (string) Full
  </code></pre>
  <p>One option I've found is to simply add a <code>tryFromName</code> function, accepting a string and looping over all the cases like this:</p>
  <pre><code>enum MainType
  {
      case Full;
      case Major;
      case Minor;
  
      public static function tryFromName(string $name): ?static
      {
          foreach (static::cases() as $case) {
              if ($case-&gt;name === $name) {
                  return $case;
              }
          }
  
          return null;
      }
  }
  
  $name = (MainType::Full)-&gt;name
  var_dump(name); // (string) Full
  var_dump(MainType::tryFromName($name)); // MainType::Full
  </code></pre>
  <p>This works, however it seams counter intuitive to enable a foreach loop going over all possibilities just to create an enum.</p>
  <p>Therefore the question is, what is the right way to get an Enum in PHP from the name.</p>