note 128924 added to class.backedenum

From: Date: Thu, 28 Sep 2023 14:25:49 +0000
Subject: note 128924 added to class.backedenum
Groups: php.notes 
Request: Send a blank email to php-notes+get-220884@lists.php.net to get a copy of this message
If you have a string and want to convert it into a enum, you may find this approach useful: <?php enum Company { case Acme; case Meac; public static function fromName(string $name): self { foreach (self::cases() as $case) { if ($name === $case->name) { return $case; } } throw new Exception("Invalid Company: $name"); } } $comany = Comany::fromName('Acme'); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 212.3.92.146) ---- Manual Page -- https://php.net/manual/en/class.backedenum.php Edit -- https://main.php.net/note/edit/128924 Del: integrated -- https://main.php.net/note/delete/128924/integrated Del: useless -- https://main.php.net/note/delete/128924/useless Del: bad code -- https://main.php.net/note/delete/128924/bad+code Del: spam -- https://main.php.net/note/delete/128924/spam Del: non-english -- https://main.php.net/note/delete/128924/non-english Del: in docs -- https://main.php.net/note/delete/128924/in+docs Del: other reasons-- https://main.php.net/note/delete/128924 Reject -- https://main.php.net/note/reject/128924 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220884) next »