note 129370 added to language.enumerations.backed

From: Date: Wed, 03 Apr 2024 05:44:54 +0000
Subject: note 129370 added to language.enumerations.backed
Groups: php.notes 
Request: Send a blank email to php-notes+get-221269@lists.php.net to get a copy of this message
<?php // Enums cannot be created by the new keyword. But if you use backed enums you can replicate something similar where you create enums from values. trait EnumCreator { public static function new(string $value): self { return self::from($value); } } enum Size: string { use EnumCreator; case Small = 'Small'; case Medium = 'Medium'; case Large = 'Large'; } var_dump(Size::new('Small')); ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.131 (proxied: 103.197.153.39) ---- Manual Page -- https://php.net/manual/en/language.enumerations.backed.php Edit -- https://main.php.net/note/edit/129370 Del: integrated -- https://main.php.net/note/delete/129370/integrated Del: useless -- https://main.php.net/note/delete/129370/useless Del: bad code -- https://main.php.net/note/delete/129370/bad+code Del: spam -- https://main.php.net/note/delete/129370/spam Del: non-english -- https://main.php.net/note/delete/129370/non-english Del: in docs -- https://main.php.net/note/delete/129370/in+docs Del: other reasons-- https://main.php.net/note/delete/129370 Reject -- https://main.php.net/note/reject/129370 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221269) next »